On 17 June 2015 at 04:46, Vincent Lequertier <s...@riseup.net> wrote:
>
> #!/usr/bin/perl
> use strict;
> use warnings;
> open my $fh, '<', 'text';
> my @words = split ' ', <$fh>;
> my @matchs;
> while (my ($index, $elem) = each @words) {
>         if ($elem eq 'bspwrt') {
>             push @matchs, $index++;
>         }
>     }
> $, = ' ';
> print @matchs;


Obviously that strategy may be limited and slow if you need to execute
the lookup > 3 times for several queries.

That can be avoided by using a hash representation of the data, etc:

my @words = split ' ', <$fh>;
my %match_index;
while (my ($index, $elem) = each @words) {
        $match_index{ $elem } = [] unless exists $match_index{ $elem };
        push @{ $match_index{$elem} }, $index;
 }
$, = '';
print @{ $match_index{'bspwrt'} };
print @{ $match_index{'tnbcch'} };

etc.



-- 
Kent

KENTNL - https://metacpan.org/author/KENTNL

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to