The structure that I demonstrated is a Hash of Array. A hash can only have one value for each key, BUT that value can be a pointer to another data structure, in this case, an anonymous array. To initialize an @variable, you would normally have something like: my @variable =(4,5,6); That puts 3 things into @variable. If you have @variable =[4,5,6];, that puts ONE thing into @variable, a POINTER an array with 3 things in it. That array with the 3 things has no given name and is called an anonymous array.

All of the things that you can do with an array apply in this situation: simple assignment, push, pop, shift, unshift, etc.. Below I showed some other code with a more explicit looking assignment with the square brackets for the anonymous array. It looks a bit "funny" because this is a assignment of a value to a hash key, but the principle is the same as simple array.

So to sum up all things in an array and push the total onto the array, we do it just like if this was a simple @var! See the code below.

I will point out that when you deference a multi-dimensional thing with a subscript, you need an extra pair of {}, hence  @{$hash{'aaa_1'}}.

hope this answers your question...

#!/usr/bin/perl -w use strict; my %hash; $hash{'aaa_1'}=[9, 8,7,6]; print "aaa_1 is : @{$hash{'aaa_1'}}\n"; #prints aaa_1 is : 9 8 7 6 my $array_ref = $hash{'aaa_1'}; #another way... print "@$array_ref\n"; #de-references array ref into array #prints 9 8 7 6 foreach my $num (@$array_ref) #another way { print "a num is: $num\n"; } #prints this... #a num is: 9 #a num is: 8 #a num is: 7 #a num is: 6 ################# $hash{'aaa_1'}=[]; #clears anon array push @{$hash{'aaa_1'}},(4,5,8,9); print "aaa_1 is : @{$hash{'aaa_1'}}\n"; #prints aaa_1 is : 4 5 8 9 ############### my $sum=0; foreach my $num (@{$hash{'aaa_1'}}) { $sum+=$num; } push @{$hash{'aaa_1'}}, $sum; print "aaa_1 is now : @{$hash{'aaa_1'}}\n"; #prints aaa_1 is now : 4 5 8 9 26

In reply to Re^5: hash help by Marshall
in thread hash help by bkish11

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.