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

This line works, but
my @all_names = map { {name => $_->{name}} } @{ $data }[1 .. $#$data];

I just need some explaining in how different one line of code is from another, why wouldn't the name=>"Accounts" be picked up by the code here:
use warnings; use strict; use Data::Dumper; my $data = [ { val => "action", name => "Accounts", id => "None", }, { name => "PA1", id => "AUTOB" }, { name => "PB3", id => "AUTOP" }, { name => "BOX", id => "BOP" }, { name => "DW0", id => "DFIRE" }, { name => "QW3", id => "HOME" }, { name => "CM7", id => "CUMBR" }, { name => "BR1", id => "PUMBR" }, { name => "TY6", id => "ECUST" }, ] ; #my @all_names = map { $_->{name} } @{ $data }[1 .. $#$data]; #my @hash_names = map{ {name=>$_} } $data; my @all_names = map { {name => $_->{name}} } @{ $data }[1 .. $#$data]; print Dumper(\@all_names); #print Dumper \@hash_names; __END__
Such a good answers , thanks!

Replies are listed 'Best First'.
Re^5: Extracting elements from array
by choroba (Cardinal) on Jan 15, 2016 at 16:22 UTC
    why wouldn't the name=>"Accounts" be picked up
    Because you start at element #1, not 0:
    [1 .. $#$data]; ^ | | Here!
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re^5: Extracting elements from array
by tangent (Parson) on Jan 15, 2016 at 17:24 UTC
    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.