Home > Mobile >  Extract data hash reference from perl array
Extract data hash reference from perl array

Time:09-15

I am new to perl. I am returning an array of references from a function. However I am lost as how I loop over the data.

sub whatever{
  my %product;
  my %resolution;
  my @data = ();
  push @data, \%product;
  push @data, \%resolution;
  return @data;
}

In the control sub module.

my @results = $whatever($dt_id);
$c->app->log->debug(Dumper(@results));

results

$VAR1 = {'IOP' => 'IOP'};
$VAR2 = {'4km' => '4km','9km' => '9km'};

I get the two hashes but how do I loop over them.

CodePudding user response:

try this:

for my $hash_ref (@results) {
  for my $key (keys %$hash_ref) {
    say "key '$key' has value '$hash_ref->{$key}'";
  }
}

CodePudding user response:

To return the two hashes seperately:

return (\%product, \%resolution);

To map over them:

my ($product, $resolution) = whatever(...);
for my $key (%$product) {
    # do something with $product->{$key};
}
  • Related