Ok, there seem to be two basic methods given here for constructing the hash and its contents:
my %ns_records = map { $_ => [ shift @addr_list, shift @ptr_list, shift @uptime_list ] } @ns_list;
and (or approximation - this was my version but I see it's already given above):
for (0..$#ns_list) { $ns_records{$ns_list[$_]} = [$addr_list[$_], $ptr_list[$_], $uptim +e_list[$_]]; }
Question is, which is better? The second is perhaps more easily understandable, and also leaves the original arrays intact in case you want them later, but I discovered when doing speed tests (1,000,000 iterations) that the first only takes 15 seconds vs 19 seconds for the second. The exact same results are given if you reverse everything and use pop:
my %ns_records = map { $_ => [ pop @addr_list, pop @ptr_list, pop @uptime_list ] } reverse @ns_list;
But what if there's larger numbers of items? Using arrays containing 1000 items each and looping 10,000 times, I got 55 seconds for map / shift, 55 seconds for map / pop, and 45 seconds accessing the arrays directly. Apparently the effort of modifying the arrays takes up additional processing time at larger numbers of items.

Bottom line though, it doesn't really matter from an efficiency standpoint which way you go. Use whichever method is more readable :)


In reply to Re: Constructing a HoA from 4 separate arrays by TedPride
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.