Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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

by rocketperl (Acolyte)
on Aug 03, 2013 at 09:12 UTC ( #1047678=perlquestion: print w/ replies, xml ) Need Help??
rocketperl has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys i basically have three arrays @cg,@gl and @csta. @gl and @csta are of equal 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. pls help. When i try this, only the first element of csta gets printed throughout. thanks
for my $g (@cg) { if (grep $_ eq $g, @gl) { push @ind,$_; print N "$g\n"; #below is where iam trying to print the #correspond +ing index val. print S "$csta[$g]\n"; }}

Comment on how to refer the index of an array to another array of the same length
Download Code
Re: how to refer the index of an array to another array of the same length
by Athanasius (Monsignor) on Aug 03, 2013 at 09:34 UTC

    I think you need a C-style for loop, to keep track of the array index. Something like this:

    for my $i (0 .. $#cg) { my $g = $cg[$i]; if ($g eq $gl[$i]) { push @ind, $g; print N "$g\n"; print S "$csta[$i]\n"; } }

    Is that what you’re looking for? If not, please specify sample input (the contents of the arrays) together with the output you expect.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Thank you so much. The logic is correct but it prints the first index of csta only. Can you suggest me a way to iterate it? Thanks a lot
      @gl LYPLA1 MSC TERF1 CFC1 COL3A1 CLK1 FZD7 ADAM23 CREB1 FN1 XRCC5 DES PAX3 GBX2 TCFCP2L1
      @csta 4797974 14746047 15795739 34592493 45368491 58480932 59539023 63492478 64579378 71699745 72354094 75356919 78193711 91827751 120524522
      @cg LYPLA1 LYPLA1 LYPLA1 LYPLA1 LYPLA1 LYPLA1 LYPLA1 LYPLA1 LYPLA1 LYPLA1 LYPLA1 LYPLA1 LYPLA1
        You are only looking for the key "LYPLA1", which happens to be the first element of @gl and therefore your result will show for every match of "LYPLA1", the first element of @csta, i.e. "4797974". You might wish to vary the search keys a bit to see something different.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics
Re: how to refer the index of an array to another array of the same length
by CountZero (Bishop) on Aug 03, 2013 at 09:55 UTC
    With a regular expression and only one loop.
    use Modern::Perl; use Regexp::Assemble; # load the arrays my @cg = ( 1, 5, 12, 3 ); my @gl = map { int( rand 20 ) + 1 } ( 1 .. 100 ); my @csta = map { chr( $_ + 64 ) } @gl; say "@gl\n@csta"; # for debug purposes only # assemble the regex my $ra = Regexp::Assemble->new; $ra->add(@cg)->anchor_word(1)->re; say $ra->as_string; # for debug purposes only # find the matches for my $idx ( keys @gl ) { say "$idx: $gl[$idx] : $csta[$idx]" if ( $gl[$idx] =~ m/$ra/ ); }
    Sample output:
    3: 5 : E 13: 1 : A 15: 3 : C 19: 3 : C 26: 1 : A 35: 5 : E 39: 5 : E 42: 5 : E 44: 5 : E 53: 5 : E 73: 1 : A 75: 1 : A 81: 12 : L 83: 5 : E

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
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
    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: perlquestion [id://1047678]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (15)
As of 2014-07-29 15:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (220 votes), past polls