How do I access array elements stored in a hash?

By : altvali

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.

By : altvali


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


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:


It is Array of hashes, try this way:


This video can help you solving your question :)
By: admin