in reply to Hash of Arrays

Try to make it base on this:
use Data::Dumper; open(DATA, "<", "test.dat"); my $data = {}; while (<DATA>) { chomp; /(.*)::=(.*)/; if (!exists($data->{$1})) { $data->{$1} = []; } $data->{$1}[$#{$data->{$1}} + 1] = $2; } print Dumper($data);

Replies are listed 'Best First'.
Re: Hash of Arrays
by jonadab (Parson) on Oct 12, 2003 at 19:29 UTC

    This will work, but is there a good reason not to simplify this code by using the 'push' builtin?


    $;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/
      actually the the reason i wanted the array is because multiple things will be stored in it...for example.... the input looks like <man> ::= <blah> <man> ::= <blah2> so, basically we can have identical keys on the left side in the input and only one string on the right. So in my hash of arrays I'd need to go through the input storing keys and if there is a duplicate key, store whats on the right side of the ::= into that key. thanks again for your help.
        the input looks like
        <man> ::= <blah> <man> ::= <blah2>

        Ah, in that case you'll want to go with either davido's solution or pg's (whichever one you understand better). I wasn't sure from your original post whether that was the case.


        $;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/
        oh i dont know how to make separate lines in this forum post...but <man>::=<blah> is one line and <man> ::= <blah2> is the second.