use Data::Dumper; $copy=1; #what you want? %threads=( keyA => [ 2, 6, 8 ], keyB => [ 3, 5, 10, 14, 18 ], ); $tnum=0; foreach $thread (keys %threads) { $tsub[$tnum] = $thread; $tlist[$tnum] = $threads{$thread} unless $copy; @{$tlist[$tnum]} = @{$threads{$thread}} if $copy; $tsize[$tnum] = @{$tlist[$tnum]}; $tnum++; } print Dumper \@tsub,\@tlist,\@tsize;
# OUTPUT
$VAR1 = [ 'keyB', 'keyA' ]; $VAR2 = [ [ '3', '5', '10', '14', '18' ], [ '2', '6', '8' ] ]; $VAR3 = [ 5, 3 ];
there are more elegant ways using arrayslices but I think you should first start to understand perlref
Cheers Rolf
UPDATE: Who cares, here a solution with slices (no copy)
@tsub2 = keys %threads; @tlist2 = @threads{@tsub2}; @tsize2 = map { scalar @{$_} } @tlist2; print Dumper \@tsub2,\@tlist2,\@tsize2;
hmmm that solution without slices is even simpler, I wasn't sure that the order of keys and values are guarantied, but it's documented so, as long as the hash isn't modified.
In reply to Re: hash of arrays to array of arrays
by LanX
in thread hash of arrays to array of arrays
by hsfrey
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |