in reply to Re: Pattern matching in perl
in thread Pattern matching in perl

If speed is a concern, creating one regex to match all the possible names instead of looping over them is much faster:
#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; my @list = ('Anls', 'core', 'route'); my $pattern = join '|', @list; $pattern = qr{^($pattern)/(.*)}; my $file = '/home/test.txt'; open my $fh, '<', $file or die "$file: $!"; while (my $line = <$fh>) { if (my ($name, $version) = $line =~ $pattern) { say "$name: $version"; } }
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]