in reply to Hashes and Arrays

Assuming I understand what you are saying, that should probably read:
push @{$hash->{IP}}, [ @row ];
Which creates a new anonymous array with the contents of @row, and pushes it into the array referenced by $hash->{IP}.

Replies are listed 'Best First'.
Re: Re: Hashes and Arrays
by datannen (Novice) on Jul 16, 2003 at 05:36 UTC
    my @rows; my @newrows; my @printrows; my %newhash; @{%newhash->{$value}}; $rows[0] = "IP"; $newrows[0] = "value1"; $newrows[1] = "value2"; push (@{%newhash->{$rows[0]}}, @newrows); push (@{%newhash->{$rows[0]}}, @newrows); print "\n"; foreach $key (sort keys %newhash) { print "key: $key\n"; @printrows = %newhash->{$key}; print $printrows[0][0]; # should be Value1? print $printrows[1][1]; # should be Value2? print "\n"; }
    tried it with:
    [ @row ]
    and printrows 0,0 just gave me an array. Thanks again.

      First of all, you should probably add a 'use strict;' at the top of your code. It will show you a couple of problems with the code you presented above.

      Second, if you are having problems with data structures, and they are not doing what you want, use Data::Dumper to see what is actually happening. Add the following code snippet after you have built your data structure:

      use Data::Dumper; print Dumper(\%newhash);

      When I ran your code with this, it gave me the following:

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

      So the code is not doing what think it is doing.

      I hope that gives you enough to continue.

      - Cees

        Thank you very much Cees. After spending a little more time with your explanations, everything works great. David
      btw, $printrows 0,3 gives the value of what I think should have gone into $printrows 1,1 -- value2. That is what I meant by everything being added to 0 rather than X.

        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