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

I am using mod 2, to find if a row is odd or even, creating a key based on the content of a field in a flatfile... it works fine, but i always get masses of errors in apache log ( win32) saying: Use of uninitialized value in modulus (%) at C:/Apache Group/Apache2/cgi-bin/blah.cgi line 281. what is it trying to tell me please! - its works so what am i doing wrong! thanks very much!
my %alphabet; my $s; code... foreach $record (@contents) { @fields = split(/\|/,$record); $tidm=$fields[0];$tidm=~s/\s+$//g; $nice=$fields[1];$nice=~s/\s+$//g; $s = substr($nice,0,1); open (DATA, ">> ./ATOZ/$s.shtml" ); if ($alphabet{$s} % 2) { print DATA qq|<tr bgcolor="#FFFFFF"></TR>\n|; } else { print DATA qq|<tr bgcolor="#F8F8F8"></TR>\n|; } close DATA; $alphabet{"$s"}++; }

Replies are listed 'Best First'.
Re: uninitialized value in modulus (%)
by fruiture (Curate) on Aug 19, 2002 at 14:28 UTC
    if ($alphabet{$s} % 2) {

    The problem must be here somehow, becase you're using the % operator only here. The warning indicates that one operand of % is undefined. 2 is a defined constant, so $alphabet{$s} must be undef. %alphabet mus have been filled somehwere else, you didn't show that code, $s comes from a very "ugly" part of your code:

    foreach $record (@contents) { # where does @contents come from? # you said a flat-file: why did you read the whole # file into an array, if you did? is that neccessary? # you'll probably rather like while(defined(my $record = <$flatfilehandle>)){ @fields = split(/\|/,$record); # @fields is global? probably not my @fields = split... $tidm=$fields[0];$tidm=~s/\s+$//g; $nice=$fields[1];$nice=~s/\s+$//g; #it's much easier to say my ($tidm,$nice) = split /\|/,$record; # and it's more effective to drop the /g, for it's # completely useless: You cannot mach the EOString more # than one time. Correct me if these variables might # contain multiple lines, but i'm "quite sure" they don't # :) $s = substr($nice,0,1);

    So now $s holds the first character of $nice. If $nice is empty, $s will be empty, too. Do you have the key '' in your hash %alphabet. $s may be any character (apart from '') depending on the input, are all cases covered in %alphabet? If not, consider assuming a default:

    (defined $alphabet{$s} ? $alphabet{$s} : 0) % 2 # use exists() if all values of %alphabet are defined
    --
    http://fruiture.de
      thanks for the help, i will have to digest this for a while! I dont think $s can be empty, as the hash is automatic i think, there are no invalid records in flatfile, i will check... i may have things confused. if so i apologise! danke
        Thanks very much, you were all quite right, the first iteration found no value ( uninitialised! doh! ) so i made it increment from 0 before i did stuff on it.. now it works fine... thanks perl monks!
        $alphabet{$s}++; open (DATA, ">> ./ATOZ/$s.shtml" )or die "can't open file: $s!"; if (($alphabet{$s}) % 2){ print DATA qq|<tr bgcolor="#eeeeee">; } else { print DATA qq|<tr bgcolor="#FFFFFF">|; } close DATA;
Re: uninitialized value in modulus (%)
by CubicSpline (Friar) on Aug 19, 2002 at 14:26 UTC
    I agree with ichimunki, the code we see looks good, but either you're getting a funky value for $s (whitespace or not alphabet character) or %alphabet doesn't really have in it what you think it does.

    I'd recommend adding a couple of sanity check statements right before your modulus test:

    # what's in the %alphabet? foreach my $letter (keys %alphabet) { print "$letter\n$alphabet{$letter}\n"; } # what's $s? print "$s\n";<br> # does a key/value pair exist in %alphabet for $s? print "$alphabet{$s}\n";<br>

    ~CubicSpline
    "No one tosses a Dwarf!"

      thanks, i think it does have all i would expect.. but maybe a whitespace exists, i will check it out, thanks A, 309 B, 318 C, 292 D, 120 0-9, 11 E, 183 F, 146 G, 164 H, 161 I, 199 J, 93 K, 54 L, 132 M, 280 N, 112 O, 50 P, 193 Q, 25 R, 128 S, 339 T, 182 U, 51 V, 46 W, 101 X, 11 Y, 22 Z, 19
Re: uninitialized value in modulus (%)
by ichimunki (Priest) on Aug 19, 2002 at 14:14 UTC
    Are you sure that %alphabet contains data? None of the code above shows us how data gets in there... and if data gets in there for likely values of $s.

    Is it issuing this warning multiple times for the same run? Otherwise: you get an error the first time through because you never set $alphabet{$s} to anything for its first use, but at the end of your sub, you ++ it, which means that thereafter it has a valid value (i.e. has been "initialized").

      Yes, multiple times... I thought i had initialized it by saying: my alphabet; and then it would populate itself automatically...? thank you!