Yoda_Oz has asked for the wisdom of the Perl Monks concerning the following question:

I cannot for the life of me understand why i keep getting the error:
Use of uninitialized value in array element at punctuation.pl line 57.
im getting the right output. the code is working properly except for that error. basically, the program is supposed to read from a txt file and spit out what punctuation is in there...
here is my code:
#!usr/local/bin/perl use strict; use warnings; my %wordHash; my @wordArray; print ("Enter filename to search for punctuation characters: "); my $path=<STDIN>; print ("\n"); open(DATA, "<$path") || die "Couldn't open $path for reading: $!\n"; while (<DATA>) { while (s/([\041-\057]|[\72-\100]|[\133-\140]|[\173-\176])(.*)/$2/) { my $count = $1; $wordHash{$count}++; } } close (DATA); my %charnames = ( '?' => 'question mark', '.' => 'fullstop', ',' => 'comma', '(' => 'open bracket', ')' => 'close bracket', '-' => 'hyphen/minus', '$' => 'dollar', '=' => 'equals', '/' => 'forward slash', "\\" => 'backward slash', '|' => 'pipe', '!' => 'exclaimation', '"' => 'speech marks', '*' => 'asterisk', '£' => 'pound', '%' => 'percent', '^' => 'carrot', '&' => 'ampes and', '_' => 'underscore', '+' => 'plus', '{' => 'open curly bracket', '}' => 'close curly bracket', '[' => 'open square bracket', ']' => 'close square bracket', '~' => 'tilde', '#' => 'hash', '<' => 'less than', '>' => 'greater than', '@' => 'at', "'" => 'apostrophe', ':' => 'semi-colon', ';' => 'colon', ); while ( my ($punctuation, $count) = each(%wordHash) ) { $wordArray[my $i] = "$count\t$punctuation"; $i++; print ("$count\t$charnames{$punctuation}\n"); }
here is the output i get with a particular file:
Enter filename to search for punctuation characters: ooperl.txt Use of uninitialized value in array element at punctuation.pl line 57. 2 hyphen/minus Use of uninitialized value in array element at punctuation.pl line 57. 1 close bracket Use of uninitialized value in array element at punctuation.pl line 57. 7 fullstop Use of uninitialized value in array element at punctuation.pl line 57. 7 comma Use of uninitialized value in array element at punctuation.pl line 57. 1 open bracket

Replies are listed 'Best First'.
Re: unitialized values - HELP!
by Fletch (Bishop) on Jan 11, 2007 at 14:44 UTC

    Well, if you look at line 57:

    55 while ( my ($punctuation, $count) = each(%wordHash) ) 56 { 57 $wordArray[my $i] = "$count\t$punctuation"; 58 $i++; 59 print ("$count\t$charnames{$punctuation}\n"); 60 }

    You'll see that in line 57 you declare a variable inside the square brackets of an array access. Each time through the loop you'll get a new $i which, since you haven't initialized it, will have an undefined value. What you probably want is to just push the next value onto @wordArray instead.

Re: unitialized values - HELP!
by davorg (Chancellor) on Jan 11, 2007 at 14:48 UTC
    while ( my ($punctuation, $count) = each(%wordHash) ) { $wordArray[my $i] = "$count\t$punctuation"; $i++; print ("$count\t$charnames{$punctuation}\n"); }

    Each time round that loop, you create a new variable called $i which is uninitialised (and therefore contains "undef"). You then use that variable as an array index. As it contains "undef", Perl converts it to a zero, but warns you that it has done so. You then increment the variable and let it go out of scope. A new (undefined) $i is created the next time round the loop.

    What you probably want is this:

    while ( my ($punctuation, $count) = each(%wordHash) ) { push @wordArray, "$count\t$punctuation" print ("$count\t$charnames{$punctuation}\n"); }

    Had you looked at the contents of @wordArray, you would have seen the problem.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg