Hello shmem,
Exactly as you said it, I am interested in the values only. But for maintenance and readability reasons I am using AoH.
I was reading about Named Arguments which describes exactly what I need. But the reason that I am using AoH is save resources by defining less scalars than before. But if I use the Named Arguments solution, I will end up where I started. Unless I am missing something.
Thank you for your time and effort reading and providing a solution to my problem.
Seeking for Perl wisdom...on the process of learning...not there...yet!
| [reply] [d/l] [select] |
But if I use the Named Arguments solution, I will end up where I started. Unless I am missing something.
You are missing something. You can use a hash to store key/value pairs instead of having many scalar variables. As you probably know, the order of hash keys retrieved by keys is arbitrary. But you can also have an array of keys, which keeps them ordered.
Using named parameters doesn't re-introduce the scalar values: you just pass the hash to the subroutine. Inside the subroutine, you can assign the passed parameters (which is a flat list of key/value pairs) to a hash and retrieve the values with the original keys.
my %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
);
my @keys = qw(
LI_VN_Mode
Stratum
Poll
Precision
Root_Delay
Dispersion
Reference_Identifier
Reference_Timestamp_Sec
Reference_Timestamp_Micro_Sec
Originate_Timestamp_Sec
Originate_Timestamp_Micro_Sec
Receive_Timestamp_Sec
Receive_Timestamp_Micro_Sec
Transmit_Timestamp_Sec
Transmit_Timestamp_Micro_Sec
);
# get values in ordered mode for call with positional parameters
my @arraySendSntpPacket = @Hash_To_Send{@keys}; # hash slice!
my $result;
# two ways of calling:
# positional parameters
$result = positional_function( @arraySendSntpPacket );
# named parameters
$result = named_param_function( %Hash_To_Send );
sub positional_function {
my @args = @_;
# do something with $args[0], $args[1], ...
}
sub named_param_function {
my %args = @_; # flattened list of key/value pairs
# do something with $args{LI_VN_Mode}, $args{Stratum}, ...
}
perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
| [reply] [d/l] |