Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How can I create a hash with 2 key value pairs that also links to an array of data? I am not sure if this is caleld a multidimensional hash of arrays.

Upon reading a file I need to compare 2 vaules for example Network and connection type.

If these values do not exist create the new values and store the trailing data with them.

I currently have the following which works with only one key pair, but it is over writing the previous data because I am only comparing one field:

@str = qw( 7 house network_name 4 6); if($myhash {$str[2]}}){ #if found // if netowrk_name is found increment the values of [3] and [4] } else{ //must be new so ad new data to hash }
But I need to compare and store 2 results.
example

I need to store and compare [0] and [2] and then have that array of data behind it.

Can someone shed some light on this for me?

thanks,
Kev

Edit by tye

Replies are listed 'Best First'.
Re: multidimensional hash of array
by Masem (Monsignor) on May 31, 2001 at 19:12 UTC
    There's two ways that you can do this in perl:

    One, create a hash of hashs of arrays:

    my %hash = ( network_type_1 => { conn_type_1 => [ 1 ], conn_type_2 => [ 2 ] }, network_type_2 => { conn_type_1 => [ 3 ], conn_type_2 => [ 4 ] } ); # To get at the elements: print $hash{ $network }->{ $connection }->[ $pos ];
    Alternatively, if there is a way to delimit the network and connection type into one string, you can get away with just a hash of arrays:
    my %hash = ( 'network-type_1|conn_type_1' => [ 1 ], 'network-type_1|conn_type_2' => [ 2 ], 'network-type_2|conn_type_1' => [ 3 ], 'network-type_2|conn_type_2' => [ 4 ] ); print $hash{ "$network|$conn" }->[ $pos ];
    While the latter is probably easier to type out, the former case is easier to understand.


    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
Re: multidimensional hash of array
by bikeNomad (Priest) on May 31, 2001 at 19:17 UTC
    Hi Kev,
    You can make an anonymous array reference using square brackets:
    my %myhash; my @str = qw( 7 house network_name 4 6); my $netName = $str[2]; if (defined($myhash{$netName})) { #if found my $arrayRef = $myhash{$netName}; $arrayRef->[0]++; # increment numbers $arrayRef->[1]++; } else { # must be new so ad new data to hash $myhash{ $netName } = [ @str[3..4] ] }