Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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 ( #1047684=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

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


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://1047684]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2014-07-25 04:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (167 votes), past polls