Zaxo,
I wonder what
riz meant by
"both @arr & file are big"? I know that
demerphq has provided patches to bleed perl for alternation in regexen, but this can get slow quickly in 5.8.x. Ok - let's assume that for some reason a hash isn't an option (order is important) and memory consumption is at a premium already. Is there a way to make it any faster?
#!/usr/bin/perl
use strict;
use warnings;
my @set = qw(one bright day in the middle of the night);
my %pos;
# Preprocess @set
{
no warnings 'uninitialized';
vec( $pos{ ord( $set[ $_ ] ) }, $_, 1 ) = 1 for 0 .. $#set;
}
while ( my $val = <DATA> ) {
chomp $val;
my $bitstr = $pos{ ord( $val ) } or next;
my $i = length( $bitstr ) * 8 - 1;
while ( $i-- ) {
print "$val\n" and last if vec($bitstr, $i, 1) && $val eq $set
+[ $i ];
}
}
__DATA__
one
two
buckle
my
shoe
the
cat
in
the
hat
I have no idea how well this scales but my guess is it is a failed attempt. I doubt the reciprocal of this solution (indexing file offsets instead) would have been any better. It was a fun experiment anyway.