If we give transpose row-wise data, it will give us back column-wise data and vice versa.sub transpose { my $rows = shift; my $max_col = @{ $rows->[0] } - 1; [ map { my $c=$_; [ map {($_->[$c])} @$rows ] } 0..$max_col ] }
With this helper function, we can easily put your input data into the desired format:# [ [ 0, 1 ] <== transpose ==> [ [ 0, 2 ] # , [ 2, 3 ] ] , [ 1, 3 ] ]
Now we have row-wise data:my $data_by_rows = transpose( [ \@ns_list, \@addr_list, \@ptr_list, \@uptime_list ] ) ;
$data_by_rows = [ [ 'server1.foo-domain.net', '1.2.3.5', '5.3.2.1.in-addr.arpa', '131 days' ], [ 'server2.noo-domain.net', '11.22.33.55', '55.33.22.11.in-addr.arpa', '28 days' ], [ 'server3.zoo-domain.net', '22.21.20.55', '55.20.21.22.in-addr.arpa', '366 days' ] ];
All that's left to do is convert each row into hostname=>[data] format and load it into your hash:
That's it!my %ns_records = map {( shift @$_, [ @$_ ] )} @$data_by_rows;
While munging the data manually isn't difficult, by factoring out the transposition part of the job, we made our solution easier to understand. We also created a handy helper function that we can reuse on other projects.
Cheers,
Tom
Tom Moertel : Blog / Talks / CPAN / LectroTest / PXSL / Coffee / Movie Rating Decoder
In reply to Re: Constructing a HoA from 4 separate arrays
by tmoertel
in thread Constructing a HoA from 4 separate arrays
by hsinclai
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |