Perl array and hash manipulation using map

Posted by somebody on Stack Overflow See other posts from Stack Overflow or by somebody
Published on 2010-03-08T13:57:42Z Indexed on 2010/03/08 15:06 UTC
Read the original article Hit count: 153

Filed under:
|
|
|

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;

The output is

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

Shouldn't the elements be "bar, linux, test"? Why is the last element undefined and how do I insert an element into @final_array? I know I can use the push function but is there a way to insert it on the same line as using the map command?

Basically the manipulated array is meant to be used in an SQL command in the actual script and I want to avoid using extra variables before that and instead do something like:

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

© Stack Overflow or respective owner

Related posts about perl

Related posts about sqlite