in reply to making a text file to an hash array
The fact that the OP seems to want anonymous arrays for all the values makes things a bit easier. You can return an AoA of key and (possibly empty) value parts from a global match in an anonymous subroutine. Pass this into a map to deal with the value string using split.
$ perl -MData::Dumper -E ' > $text = q{a 2 b 4,5,6 c d e 45,657,-67}; > %hash = > map { $_->[ 0 ] => [ split m{,}, $_->[ 1 ] ] } > sub { > push @arr, [ $1, $2 ] > while $_[ 0 ] =~ m{\s*([a-z])\s+([-,\d]*)}g; > return @arr; > }->( $text ); > say Data::Dumper->Dumpxs( [ \ %hash ], [ qw{ *hash } ] );' %hash = ( 'e' => [ '45', '657', '-67' ], 'c' => [], 'a' => [ '2' ], 'b' => [ '4', '5', '6' ], 'd' => [] ); $
I hope this is helpful.
Cheers,
JohnGG
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: making a text file to an hash array
by changma_ha (Sexton) on Jul 15, 2010 at 04:07 UTC | |
by johngg (Canon) on Jul 15, 2010 at 20:36 UTC |