in reply to Pattern matching in perl
Because this is Perl, There Is More Than One Way To Do It:
use strict;
use warnings;
my $file = "/home/test.txt";
# Store the list of desired names in a hash, with a true value for each.
my %list = map { $_ => 1 } qw{ Anls core route };
open my $fh, '<', $file or die "Failed to open $file: $!";
while ( <$fh> ) {
# Use split() to break $_ on slashes. Save only first two parts.
my ( $x, $version ) = split /\//;
# Print if hash entry {$x} contains a true value. Non-existing entries are false.
print "$x: version $version\n" if $list{$x};
}
close $fh;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Pattern matching in perl
by Fletch (Bishop) on Jul 01, 2022 at 15:18 UTC | |
by Anonymous Monk on Jul 02, 2022 at 17:32 UTC |