Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
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 lurking in the Monastery: (5)
As of 2014-09-16 01:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (155 votes), past polls