Wise ones,

The following is academic, and may well never occur in the real world, and I haven't seen this arrangement in the data structure discussions I've read so far around PM.

As in the table below, if you assume we have four arrays, each containing data about different hosts: an array of hostnames, another of IP addresses, another of PTR records, another of uptimes. If you imagine a database table each array would look like a column from the table. (Could we assume, since this is academic, that the data is sorted correctly and corresponds to each host?)

Array 1 Array 2 Array 3 Array 4
hostname1 1.2.3.5 5.3.2.1.in-addr.arpa 131 days
hostname2 11.22.33.55 55.33.22.11.in-addr.arpa 128 days
hostname3 22.21.20.55 55.20.21.22.in-addr.arpa 366 days

If you could arrange the arrays vertically as if they were columns and join them, you could reconstruct the table as it is seen above. From each record (light blue font) in this imaginary table, I'd like to add a hash entry to %ns_records containing the hostname as the key, and an array of the other values as the value of the key.

This code creates a hash using the hostnames as keys, and creating an array of the values from the correct elements of each corresponding array.

#!/usr/bin/perl -w use strict; use Data::Dumper; my @ns_list = ( 'server1.foo-domain.net', 'server2.noo-domain.net', 'server3.zoo-domain.net', ); my @addr_list = ( '1.2.3.5', '11.22.33.55', '22.21.20.55', ); my @ptr_list = ( '5.3.2.1.in-addr.arpa', '55.33.22.11.in-addr.arpa', '55.20.21.22.in-addr.arpa', ); my @uptime_list = ( '131 days', '28 days', '366 days', ); my %ns_records = map { $_ => [ shift @addr_list, shift @ptr_list, shift @uptime_list ] } @ns_list; my $d; foreach my $k ( keys %ns_records ) { print "\n$k:"; for ( $d = 0; $d <= $#{$ns_records{$k}}; $d++ ) { print "\n ", @{$ns_records{$k}}->[$d]; } print $/; } print $/; __END__ print Dumper \%ns_records; $VAR1 = { 'ns0.foo-domain.com' => [ '1.2.3.5', '5.3.2.1.in-addr.arpa', '131 days' ], 'ns0.baz-domain.org' => [ '22.21.20.55', '55.20.21.22.in-addr.arpa', '366 days' ], 'ns0.bar-domain.net' => [ '11.22.33.55', '55.33.22.11.in-addr.arpa', '28 days' ] };
So, is there any golf that can be done on the block containing map - or a more efficient/elegant approach to achieve the same result?

Also, I don't understand why I get the error
"Using an array as a reference is deprecated"
when printing out the result, but the code still runs with Perl 5.8.1. I'd like to be able to dereference the array inside the HoA without error.

Thanks to all !


In reply to 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.