jokerzy has asked for the wisdom of the Perl Monks concerning the following question:
Then, I would like to compare each value of the hash with that of the previous ones, eg, comparing $valb with $vala, $vale with $vald and so on (comparison only within the same key of main_hash). My Perl code:#%main_hash -> $keyA -> index[0] -> {$keya => $vala} # -> index[1] -> {$keyb => $valb} # -> index[2] -> {$keyc => $valc} # # -> $keyB -> index[0] -> {$keyd => $vald} # -> index[1] -> {$keye => $vale} # -> index[2] -> {$keyf => $valf}
Now, the code above works fine, and I'm able to print out the data to the output file just like how I want it to be formatted. However, if I uncomment the line above "# <== placeholder ==>", it gives the "Use of unitialized value in print" errors.# # initializing variables, opening and reading files, etc # # a for loop to parse the file using regex $hash{$key}[$index] = {$key_1 => $val_1}; # end of for loop # writing out the data foreach my $k (sort keys(%main_hash)) { for my ($x = 0; $x < scalar(@{$main_hash{$k}}); $x++) { last if ($x + 1 >= scalar(@{$main_hash{$k}})); my ($first_key, $first_val) = each(%{$main_hash{$k}[$x]}; # take note of the lines below, where I intend to compare # $next_val to $first_val to see which has a larger value, # and if so, output a message saying that it's larger, and so on # my ($next_key, $next_val) = each(%{$main_hash{$k}[$x+1]}); # <== placeholder ==> # followed by some comparison here, eg if next_val > first_val # then do something (you get it) print OUTFILE "$first_key : $first_val\n"; } } # # some other things to do #
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Question about dereferencing multi-dimensional array/hash
by frozenwithjoy (Priest) on Jun 18, 2012 at 05:12 UTC | |
by jokerzy (Initiate) on Jun 18, 2012 at 05:36 UTC | |
|
Re: Question about dereferencing multi-dimensional array/hash
by Athanasius (Archbishop) on Jun 18, 2012 at 07:16 UTC | |
by jokerzy (Initiate) on Jun 19, 2012 at 02:37 UTC |