Your tabular data is in column-wise format, which isn't very convenient for loading into a hash, row by row. Let's create a helper function to transpose the data into row-wise format:
sub transpose { my $rows = shift; my $max_col = @{ $rows->[0] } - 1; [ map { my $c=$_; [ map {($_->[$c])} @$rows ] } 0..$max_col ] }
If we give transpose row-wise data, it will give us back column-wise data and vice versa.
# [ [ 0, 1 ] <== transpose ==> [ [ 0, 2 ] # , [ 2, 3 ] ] , [ 1, 3 ] ]
With this helper function, we can easily put your input data into the desired format:
my $data_by_rows = transpose( [ \@ns_list, \@addr_list, \@ptr_list, \@uptime_list ] ) ;
Now we have row-wise data:
$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:

my %ns_records = map {( shift @$_, [ @$_ ] )} @$data_by_rows;
That's it!

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


In reply to Re: Constructing a HoA from 4 separate arrays by tmoertel
in thread Constructing a HoA from 4 separate arrays by hsinclai

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.