Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
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 ( [id://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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1047684]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2024-04-18 02:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found