1

I'm working on a script and trying to get some values from an array stored in a hash. After searching on Google, searching for questions on SO (and there are some with similar titles but which have remained unsolved or solve problems a little bit different than mine), and after checking out the Data Structures Cookbook and trying everything reasonable to try, I've come to ask your help.

I have a hash, $action, and an array, $action->{'Events'}. Here's the output for print Dumper($action->{'Events'});:

$VAR1 = [{
    'Muted'        => 'something',
    'Role'         => 'something',
    'Event'        => 'something',
    'Channel'      => 'something',
    'Talking'      => 'something',
    'UserNumber'   => 'somenumber',
    'CallerIDName' => 'somenumber',
    'Conference'   => 'somenumber',
    'MarkedUser'   => 'something',
    'ActionID'     => 'somenumber',
    'CallerIDNum'  => 'somenumber',
    'Admin'        => 'something'
}];

I need to get, for example, the value of $action->{'EVENTS'}->{'CallerIDName'}, but this syntax and many other won't work. I've even tried $action->{'EVENTS'}[6] and $action->{'EVENTS'}->[6] and so on.

3 Answers 3

4

It is Array of hashes, try this way:

$action->{'EVENTS'}[0]->{'CallerIDName'}

see perldsc for more detail.


Updated example like:

use strict;
use warnings;
use Data::Dumper;
my $action = {};
$action->{'Events'} = [{'Muted' => 'something',
            'Role' => 'something',
            'Event' => 'something',
            'Channel' => 'something',
            'Talking' => 'something',
            'UserNumber' => 'somenumber',
            'CallerIDName' => 'somenumber',
            'Conference' => 'somenumber',
            'MarkedUser' => 'something',
            'ActionID' => 'somenumber',
            'CallerIDNum' => 'somenumber',
            'Admin' => 'something'}];
#push hash into the array of hashes
push(@{$action->{'Events'}},{'Muted' => 'something',
              'Role' => 'something1',
              'Event' => 'something1',
              'Channel' => 'something1',
              'Talking' => 'something1',
              'UserNumber' => 'somenumber1',
              'CallerIDName' => 'somenumber1',
              'Conference' => 'somenumber1',
              'MarkedUser' => 'something1',
              'ActionID' => 'somenumber1',
              'CallerIDNum' => 'somenumber1',
              'Admin' => 'something1'} );
 for(my $i=0; $i < @{$action->{'Events'}}; $i++){
    print Dumper($action->{Events}[$i]); #print entire hash in array index $i
    #print callerIDName key(any key) of each hash
    print Dumper($action->{'Events'}[$i]->{'CallerIDName'});  
 }
Sign up to request clarification or add additional context in comments.

2 Comments

can you help me further by providing a way to iterate through the hashes in $action->{'Events'} ?
thank you, i've already found a way to iterate when you've wrote to post another topic. But your effort is much appreciated
2

The one you're missing is that the $action contains a reference to an array, so the next part must dereference the array. Then within that is a hash, and you need to dereference the hash. So it should look like this:

$action->{'EVENTS'}[0]{'CallerIDname'}

(note that the ->'s beyond the first are optional, so this is fine as well:

$action->{'EVENTS'}->[0]->{'CallerIDname'}

And does the exact same thing)

Comments

1

The [ ] on the outside indicate the hash is inside an array. So try:

  $action->{Events}->[0]->{CallerIDName}

You can omit the -> between the {Events} and [0], but I prefer it for clarity. It doesn't make a difference here, but it does in other places. Compare:

  @array = (1,2,3); 
  $arrayref = \@array; 
  print $arrayref[0];    # accesses non-existent array @arrayref

  print $arrayref->[0];  # '1'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.