开发者

Perl - how to avoid warnings about uninitialized elements of an array?

开发者 https://www.devze.com 2023-02-04 14:17 出处:网络
#!/usr/bin/perl use strict; use warnings; sub paragraph { open my $file, \"<\", \"dict.txt\" or die \"$!\";
#!/usr/bin/perl
use strict;
use warnings;

sub paragraph
{
open my $file, "<", "dict.txt" or die "$!";
my @words = <$file>;
close $file;

print "Number of lines:";
my $lines = <>;

print "Max words per line:";
my $range = <>;

for(my $i = 0; $i<$lines; $i++){
    my $wordcount = int(rand($range));

        for(my $s = 0; $s<=$wordcount; $s++){
            my $range2 = scalar(@words);
            my $word = int(rand($range2));
            print $words[$word]." ";

                if($s==$wordcount){
                    print "\n";}
                    }
                    }
                    }
par开发者_StackOverflow社区agraph;

I'm trying to learn programming, so I just wrote this simple script.

When running this code, I am getting use of uninitialized value errors... I can't figure out why, but I sure I am just overlooking something.


These two lines open the dict.txt file for writing and then try to read from it.

open FILE, ">dict.txt" or die $!;
my @words = <FILE>;

Since you can't read from a write-only file, it fails. If the file was writable, then it is empty now - sorry about your nice word list. Suggestion:

open my $file, "<", "dict.txt" or die "$!";
my @words = <$file>;
close $file;

Also, please learn to indent your braces in an orthodox fashion, such as:

sub go
{
    print "Number of lines:";
    my $lines = <>;
    print "Max words per line:";
    my $range = <>;
    for (my $i = 0; $i<$lines; $i++){
        my $wordcount = int(rand($range));
        for (my $s = 0; $s<$wordcount; $s++){
            my $range2 = 23496;
            my $word = int(rand($range2));
            my $chosen = @words[$word];
            print "$chosen ";
            if ($s=$wordcount){
                print "\n";
            }
        }
    }
}

Also leave a space between 'if' or 'for' and the open parenthesis.

Your assignment if ($s = $wordcount) probably isn't what you intended; however, the condition if ($s == $wordcount) will always be false since it is in the scope of a loop with the condition $s < $wordcount. You need to rethink that part of your logic.

On average, you should choose a better name for your function than go. Also, it is probably better to invoke it as go();.


When I test compile your script, Perl warns about:

Scalar value @words[$word] better written as $words[$word] at xx.pl line 19.

You should fix such errors before posting.

You have:

my $range2 = 23496;
my $word = int(rand($range2));

Unless you have more than 23,496 words in your dictionary, you will likely be accessing an uninitialized word. You should probably use:

my $range2 = scalar(@words);

That then just leaves you with some logic problems to resolve.


Given 'dict.txt' containing:

word1
word2
word3
word4
nibelung
abyssinia
tirade
pearl

And 'xx.pl' containing:

#!/usr/bin/env perl

use strict;
use warnings;

open my $file, "<", "dict.txt" or die $!;
my @words = <$file>;
close $file;

sub go
{
    print "Number of lines: ";
    my $lines = <>;
    print "Max words per line: ";
    my $range = <>;
    my $range2 = scalar(@words);
    for (1..$lines)
    {
        for (1..$range)
        {
            my $index = int(rand($range2));
            my $chosen = $words[$index];
            chomp $chosen;
            print "$chosen ";
        }
        print "\n";
    }
}

go();

When I run it, I get:

$ perl xx.pl
Number of lines: 3
Max words per line: 4
word4 word3 word4 nibelung 
abyssinia pearl word1 tirade 
word3 word1 word3 word2 
$


Some more bugs:

        if($s=$wordcount){

You need == here.

0

精彩评论

暂无评论...
验证码 换一张
取 消