You are talking about a hash of arrays in your original question, but it looks like you really want an array of arrays... Is this correct?

In your code you have the following:

push (@{%newhash->{$rows[0]}}, @newrows); push (@{%newhash->{$rows[0]}}, @newrows);

We can simplify this to the following since $rows[0] = 'IP' and does not change (Do you maybe want a real IP address here?):

push @{$newhash{IP}}, @newrows); push @{$newhash{IP}}, @newrows);

Which is essentially saying that you want to treat the value at $newhash{IP} as an array reference, and you want to push the values in @newrows onto the end that array.

If you want this to be a two dimensional array, then you need to take Paladin's advice and push a reference to the array instead of pushing the values of the array.

push @{$newhash{IP}}, [@newrows]); push @{$newhash{IP}}, [@newrows]);

That will make copies of the array, and push a reference to the new array (you could also use \@newrows). Using Data::Dumper, the above will show up as:

$VAR1 = { 'IP' => [ [ 'value1', 'value2' ], [ 'value1', 'value2' ] ] };

Is this what you are after?

Now the loop at the end where you are trying to read the values will require some changes as well:

@printrows = $newhash{$key}; # wrong -> the hash will contain a refere +nce to an array @printrows = @{$newhash{$key}}; # right -> de-reference the array firs +t
After those changes, your code prints what you seem to be asking for. Although I am still not 100% sure this is what you are really after...

- Cees


In reply to Re: Re: Hashes and Arrays by cees
in thread Hashes and Arrays by datannen

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.