Dear Perl monks, I'm new to Perl and struggling with a hash of arrays I've created. Specifically I'm trying to use a foreach loop to loop through the keys of the hash of arrays and then delete elements of an array if they meet a certain criteria (- if two array elements corresponding to two particular keys in the hash of arrays are the same). First I create a normal hash called %phash using data from one file, then I read in another file to create the hash of arrays, called %harry, I'm printing the values I wish to delete first in order to check that I'm accessing the correct element of each array:
foreach $k (keys %harry) { $acount = 0; $bcount = 0; if (exists($phash{$k})) { foreach $a(@{$harry{$k}}) { foreach $b(@{$harry{$phash{$k}}}) { if ($a eq $b) { print "$b\n"; #this works as I expect print "$a\n"; #this works as I expect print "${$harry{$phash{$k}}}[$bcount]\n"; #this returns error Use of uninitialized #value in concatenation (.) or string print "${$harry{$k}}[$acount]\n"; #this works as I expect delete ${$harry{$phash{$k}}}[$bcount]; delete ${$harry{$k}}[$acount]; $deletions = $deletions +2; } $bcount = $bcount +1; } $acount = $acount +1; } } }
I'm confused by the error since when I access the element ${$harry{$k}}$acount this works fine, but if I substitute $k for the value contained in $phash{$k} then the value is apparently uninitialised. I know this can't be the case as $b prints fine so I'm wondering if there is something I'm missing with regards to accessing array elements in a hash of arrays? Thanks in advance for any help and sorry if it's something really simple I'm missing.

In reply to Accessing (deleting) array elements in a hash of arrays by onslaught

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.