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