Hello Monks,

I was playing around with ARRAYS OF HASHES in order to reduce the defined scalars on my script. I manage to get to the point that I want with a working solution but I am wondering if I can do the same work with less lines of code.

I am using an Array of Hashes to pass the values that I want. Then I am trying to extract the values into an array. I will apply the reversed process afterwards (values map to an Array of Hashes).

For the moment I am using two for loops to extract the values from an Array of Hashes, but I was wondering if I can use map to do the same thing in one line instead of having two loops.

Sample of code and output:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Time::HiRes qw( gettimeofday ); my @Array_Hash_To_Send = ( { LI_VN_Mode => '00100011' }, # 8 bit { Stratum => '0' }, # 8 bit { Poll => '0' }, # 8 bit { Precision => '0' }, # 8 bit { Root_Delay => '0' }, # 32 bit { Dispersion => '0' }, # 32 bit { Reference_Identifier => '0' }, # 32 bit { Reference_Timestamp_Sec => '0' }, # 32 bit { Reference_Timestamp_Micro_Sec => '0' }, # 32 bit { Originate_Timestamp_Sec => '0' }, # 32 bit { Originate_Timestamp_Micro_Sec => '0' }, # 32 bit { Receive_Timestamp_Sec => '0' }, # 32 bit { Receive_Timestamp_Micro_Sec => '0' }, # 32 bit { Transmit_Timestamp_Sec => '0' }, # 32 bit { Transmit_Timestamp_Micro_Sec => '0' }, # 32 bit ); ($Array_Hash_To_Send[13]{Transmit_Timestamp_Sec} , $Array_Hash_To_Send +[14]{Transmit_Timestamp_Micro_Sec}) = gettimeofday(); # For demonstration purposes only my @arraySendSntpPacket = ( $Array_Hash_To_Send[0]{LI_VN_Mode}, $Array_Hash_To_Send[1]{Stratum}, $Array_Hash_To_Send[2]{Poll}, $Array_Hash_To_Send[3]{Precision}, $Array_Hash_To_Send[4]{Root_Delay}, $Array_Hash_To_Send[5]{Dispersion}, $Array_Hash_To_Send[6]{Reference_Identifier}, $Array_Hash_To_Send[7]{Reference_Timestamp_Sec}, $Array_Hash_To_Send[8]{Reference_Timestamp_Micro_Sec}, $Array_Hash_To_Send[9]{Originate_Timestamp_Sec}, $Array_Hash_To_Send[10]{Originate_Timestamp_Micro_Sec} +, $Array_Hash_To_Send[11]{Receive_Timestamp_Sec}, $Array_Hash_To_Send[12]{Receive_Timestamp_Micro_Sec}, $Array_Hash_To_Send[13]{Transmit_Timestamp_Sec}, $Array_Hash_To_Send[14]{Transmit_Timestamp_Micro_Sec} +); my @Test_Array; foreach my $href ( @Array_Hash_To_Send ) { foreach my $role ( keys %$href ) { push @Test_Array, $href->{$role}; } } my @Test_Array_Values = map { } keys print Dumper \@arraySendSntpPacket; print Dumper \@Test_Array; my @Test_Array_Values = values @Array_Hash_To_Send; __END__ $VAR1 = [ '00100011', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', 1437578739, 958207 ]; $VAR1 = [ '00100011', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', 1437578739, 958207 ];

Thank you in advance for your time and effort reading and replying to my question.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to [SOLVED] How to extract the values of an Array of Hashes by thanos1983

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.