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

Re: how to refer the index of an array to another array of the same length

by rjt (Deacon)
on Aug 03, 2013 at 10:41 UTC ( #1047692=note: print w/ replies, xml ) Need Help??


in reply to how to refer the index of an array to another array of the same length

I want to print all the occurrence of @cg in @gl and for the corresponding index of matched @gl, i want to print @csta

Sounds like a perfect job for hashes, especially if your inputs are non-trivially large. Given @gl and @csta are of equal length, List::MoreUtils will work nicely:

#!/usr/bin/env perl use 5.012; use warnings FATAL => 'all'; use List::MoreUtils qw/zip/; my @cg = (qw<one two three four five>); my @gl = (qw<two four five>); my @csta = (qw<II IV V>); my %gl_csta = (zip @gl, @csta); printf "%5s => %s\n", $_, $gl_csta{$_} for grep { exists $gl_csta{$_} } @cg;

Output:

two => II four => IV five => V


Comment on Re: how to refer the index of an array to another array of the same length
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (14)
As of 2015-07-07 19:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls