sakhunzai
sakhunzai

Reputation: 14490

What is wrong with this perl associative array?

script A

use strict;

our %pre_pkg_configs;

$pre_pkg_configs{locDbList}={qw(default default_test)};

script B

//load script A

my @locDbNames = ();

foreach my $dbName ($pre_pkg_configs{"locDbList"}){
  print $dbName;
  push(@locDbNames,$dbName);
}

output

HASH(0x119b368)

I was expecting values : default default_test

Upvotes: 0

Views: 79

Answers (1)

Quentin
Quentin

Reputation: 943759

{ ... } creates a reference to a hash (what you are calling an associative array).

If you print a reference, you get output like HASH(0x119b368)

It sounds like you want an array, so use an arrayref instead of a hashref:

$pre_pkg_configs{locDbList}=[ qw(default default_test) ];

Then, when printing it, you need to convert the arrayref into an array:

foreach my $dbName (@{$pre_pkg_configs{"locDbList"}}){

Upvotes: 5

Related Questions