Greetings monks,

I have a problem here that I need help with.

My Perl code reads from a file and extracts the required data, storing them in the hierarchy as below. (I used Data::Dumper to print the hash out, and I think it's a hash of arrays of (ref to) hashes, correct?)
#%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}
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:
# # 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 #
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.

I think I'm missing something here. Is that a pseudo-hash I'm using here? Or did I do anything wrong when dereferencing the hashes?

** Update **
Fixed some parentheses' typos. Thanks to frozenwithjoy for pointing out. I actually typed them out because the actual code is quite cluttered. My apologies.


In reply to Question about dereferencing multi-dimensional array/hash by jokerzy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.