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

Helpful Ones,

I am curious if there is there a way to hash a number of arrays.
In other words some thing like:
%station_data = (1, @station1, 2, @station2, 3, @station3, 4 ,@station4, 5, @station5, 6, @station6, 7, @station7, 8, @station8, 9, @station9);
I am guessing that there is not.
In this case does anyone have a cool alternative.

Shine On,
Pearte

Replies are listed 'Best First'.
Re: %Hashing Arrays
by fpi (Monk) on Mar 18, 2001 at 12:54 UTC
    In addition to what dkubb wrote, after you store your arrays as references in a hash, you can also retrieve your arrays by dereferencing.(Better read up on references, because they may get confusing sometimes, even to an intermediate user).

    If you retrieve the hash value, you get the referenced array. To get the array, you have to dereference it. i.e.
    $station1_arrayref = $station_data{1}; @station1_array = @$station1_arrayref;

    or just use
    @station1_array = @{$station_data{1}};
    for example, to retrieve all elements of station5:
    foreach my $element (@{$station_data{5}}) { print $element. "\n"; }

    In your case, you are using sequential numbers as the keys in the hash. Depending on your situation, you could even just store the arrays within a big array:
    @station_data = (\@station1,\@station2,\@station3);
    and then retrieve the arrays in a similar manner (but remember that the index number of station1 is 0).

    @station1_array = @{$station_data[0]};

    This is one of those topics that I'm sure the PerlMonks users can give you tons of tips and shortcuts....

    (Modified to eliminate the duplicate info provided by dkubb, since we apparently posted at the same time...)
(dkubb) Re: (2) %Hashing Arrays
by dkubb (Deacon) on Mar 18, 2001 at 12:54 UTC

    You could make this a hash of array references, like this:

    my %station_data = ( 1 => \@station1, 2 => \@station2, 3 => \@station3, 4 => \@station4, 5 => \@station5, 6 => \@station6, 7 => \@station7, 8 => \@station8, 9 => \@station9, );

    For example, if you need to access the second element inside @station5, through the new %station hash, you would say:

    print $station_data{5}[1];
Re: %Hashing Arrays
by MeowChow (Vicar) on Mar 19, 2001 at 01:08 UTC
    In addition to what has already been suggested, you may want to create your array references as copies like so:
    %station_data = (1, [@station1], 2, [@station2], 3, [@station3], ...
    This is useful if you'd rather not store a reference to the original @stationX array (note that are not these deep copies, however). Also, there is quite a bit of repetition in your code. If you're feeling adventurous, consider reducing it to:
    { no strict 'refs'; $station_data{$_} = \@{"station$_"} for 0..9; }
    Although, the fact that you would want to consider disabling strict (ie. the fact that you have sequentially numbered variables @station(1..9)) is often indicative of a poor design choice in your code. Ask yourself how these sequential list variables got there in the first place, and how you could employ a reference-based data structure like a hash of arrays (or preferably array of arrays) from the beginning.

    Additionally, read up on perlreftut, perllol, perldsc, and perlref. Teach a man to fish, ya know...

Re: %Hashing Arrays
by brainheart (Initiate) on Mar 19, 2001 at 05:22 UTC

    Amen to all the above...

    I found I never really got complex data structures (like the hash of arrays you describe) in perl until I started messing around with the Data::Dumper module.

    It contains a routine called Dumper() which takes a reference and displays its contents as attractively indented perl code.

    So perldoc Data::Dumper as soon as you get the chance!