Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Find odd/even elements of array

by johngg (Abbot)
on Oct 09, 2011 at 21:39 UTC ( #930499=note: print w/ replies, xml ) Need Help??


in reply to Find odd/even elements of array

Perhaps you could turn the array containing a flat list of ids and names into an AoH and use grep to pull out the odd ids when building it.

knoppix@Microknoppix:~$ perl -MData::Dumper -E ' > @studentInfo = qw{ > 001 John > 002 Mary > 003 Tom > }; > @oddStudents = > map { { id => $_->[ 0 ], name => $_->[ 1 ] } } > grep { $_->[ 0 ] % 2 } > sub { > push @ret, [ shift, shift ] while @_; > return @ret; > }->( @studentInfo ); > say Data::Dumper->Dumpxs( > [ \ @oddStudents ], > [ qw{ *oddStudents } ] > );' @oddStudents = ( { 'name' => 'John', 'id' => '001' }, { 'name' => 'Tom', 'id' => '003' } ); knoppix@Microknoppix:~$

I hope this is helpful.

Cheers,

JohnGG


Comment on Re: Find odd/even elements of array
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2014-07-13 10:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (249 votes), past polls