in reply to uninitialized value in modulus (%)

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

Replies are listed 'Best First'.
Re: Re: uninitialized value in modulus (%)
by alexiskb (Acolyte) on Aug 19, 2002 at 14:49 UTC
    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;