Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^5: Extracting elements from array

by tangent (Parson)
on Jan 15, 2016 at 17:24 UTC ( [id://1152870]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Extracting elements from array
in thread Extracting elements from array

why wouldn't the name=>"Accounts" be picked up
In your original data the first element of the array is unwanted so toolic has @{ $data }[1 .. $#$data] which has the effect of skipping that first element. Arrays are indexed from "0" so the first element is $array[0], the second element is $array[1] and so on. So, @{ $data }[1 .. $#$data] means the range of elements starting at the second element and up to the last element.

A better way might be to use grep in combination with map:

my @all_names = map { {name => $_->{name}} } grep { exists $_->{name} } @$data;
Here grep first filters all the elements, discarding any which don't have a 'name' key, and then map constructs the desired data structure from the remaining elements.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-24 23:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found