Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1047692]
help
Chatterbox?
[Tux]: chacham I did not want to also list Committed changes to the core or core modules and (Co-)Organized a perl event
[Corion]: Tux: (Co-)organizing a Perl event doesn't make you know Perl better, but only Perl people ;)

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (13)
As of 2017-05-30 14:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?