Perl array and map hash manipulation

I have the following test code

use Data::Dumper;

my $hash = {
            foo => 'bar',
            os  => 'linux'
           };

my @keys = qw (foo os);

my $extra = 'test';

my @final_array = (map {$hash->{$_}} @keys,$extra);

print Dumper \@final_array;

Output signal

$VAR1 = [
          'bar',
          'linux',
          undef
        ];

Should the elements be "bar, linux, test"? Why is the last element undefined and how to insert an element in @final_array? I know that I can use the push function, but is there a way to insert it on the same line as with the map command?

Basically, a managed array is intended to be used in a SQL command in an actual script, and I want to avoid using additional variables before that and instead do something like:

$sql->execute(map {$hash->{$_}} @keys,$extra);
+3
source share
2 answers

$extra map, test , undef. map, :

$sql->execute((map {$hash->{$_}} @keys),$extra);
+6

-, :

my @final_array = (@$hash{@keys}, $extra);
+6

Source: https://habr.com/ru/post/1735929/


All Articles