in reply to Hash of arrays

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

Replies are listed 'Best First'.
Re: Re: Hash of arrays
by broquaint (Abbot) on Nov 04, 2002 at 14:18 UTC
    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

        Four!
        %h=map{split/: |;|\n/;shift@_,[@_]}<DATA>;

        HTH

        _________
        broquaint

        Not quite as good as broquaint's score, but close:

        y/:;/ /,split,$i=shift@_,$h{$i}=[@_]for<DATA>;

        Points to anybody who can explain why $above_code =~ s/$i=shift@_,$h{$i}=/$h{shift()}=/ won't work the same as $above_code.
        --

        Love justice; desire mercy.
        Shioo.
        my %h = map { chomp; /:\s+/ ? ( $` => [split ';', $'] ) : () } <DATA>;
        Or, perhaps a little more comprehensible (but less safe):
        my %h = map { $_->[0] => [split ';', $_->[1]] } map { chomp; [split /:\s+/] } <DATA>;