Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^3: Looping trough Array Key/Value Pairs

by AnomalousMonk (Archbishop)
on Sep 25, 2017 at 23:38 UTC ( [id://1200078]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Looping trough Array Key/Value Pairs
in thread Looping trough Array Key/Value Pairs

@testarray = { '_shards' => { 'skipped' => 0, 'successful' => 5, 'total' => 5, 'failed' => 0 }, ... };
And in that case what you mentioned dont work. I see you use $VAR1 (which works) but the goal would be to loop through an array with many elements....

A mental adjustment is needed here: initialized as shown,  @testarray is an array with a single element, an anonymous hash reference. If you can accommodate this properly in your code (or properly deal with an array with multiple elements), you will be on your way to your goal.

Remember: Data::Dumper and Data::Dump (my favorite) are your friends.

Update: Try this untested variation to haukex's posted code:

for my $hashref (@testarray) { for my $hit ( @{ $hashref->{hits}{hits} } ) { print "hit: '$hit->{_id}'\n"; for my $key ( keys %{ $hit->{_source} } ) { my $val = $hit->{_source}{$key}; print "key: '$key', val: '$val'\n"; } } }


Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1200078]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-20 01:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found