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 (Curate)
on Aug 03, 2013 at 10:41 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1047692]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-19 14:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found