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.
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |