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

Hello Monks

I have checked all previous discussion about how to create hash of array from a text file and still have problem creating it
The size of My_array for each key is different (1 or more elements)

Example: text file
t-98: 12;3;56;24
t-56: 1;98
t-3 : 12;56
...

Could you please address me, or refer me to a previous post? as I said the posts I have checked out have arrays with the same lenght, even with identifiers likes password, address, lastname, etc.

Thanks

Replies are listed 'Best First'.
Re: Hash of arrays
by tachyon (Chancellor) on Nov 04, 2002 at 14:00 UTC
    use Data::Dumper; use strict; my %hash; while (<DATA>) { chomp; next unless $_; my @bits = split /: |;/; my $key = shift @bits; $hash{$key} = \@bits; } print Dumper \%hash; __DATA__ t-98: 12;3;56;24 t-56: 1;98 t-3 : 12;56

    This generates the desired data structure:

    $VAR1 = { 't-98' => [ '12', '3', '56', '24' ], 't-3 ' => [ '12', '56' ], 't-56' => [ '1', '98' ] };

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      A variation on a theme
      use strict; use Data::Dumper; my %hoa; while(<DATA>) { chomp; my($k, $v) = split /\s*:\s*/ => $_, 2; $hoa{$k} = [ split ';' => $v ]; } print Dumper(\%hoa); __DATA__ t-98: 12;3;56;24 t-56: 1;98 t-3 : 12;56

      HTH

      _________
      broquaint

        Golf.......

        do{split/: |;|\n/;$h{$_[0]}=[@_[1..$#_]]}for<DATA>

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Hash of arrays
by rdfield (Priest) on Nov 04, 2002 at 13:58 UTC
    There are many, many nodes in the Monestary explaining the details of hashes of arrays, but your best place to start would be perldsc, I believe.

    rdfield

Re: Hash of arrays
by Anonymous Monk on Nov 04, 2002 at 14:52 UTC
    Thanks Monk
    I already check the documentation about hash, and your code examples and variations, and now it is clear for me... and my program is getting there :)!!

      You may want to check out my posting of almost the same question a few weeks back at Hash Of Arrays

      Hope that helps

      M