in reply to Looping in a Loop
Hello Violet, and welcome to the Monastery!
Although there is nothing wrong with your approach, a more idiomatic or “Perlish” way to do this would be to use a hash for the look-up. Here’s a little script to illustrate:
#! perl use strict; use warnings; my @list = qw( Wilma Betty ); my %lookup = map { $_ => undef } @list; while (my $line = <DATA>) { my @fields = split /\s+/, $line; next unless defined $fields[1]; print $line if exists $lookup{$fields[1]}; } __DATA__ Fred Wilma Pebbles Dino Barney Betty Bamm-Bamm Homer Marge Bart Lisa Maggie George
Output:
23:18 >perl 1015_SoPW.pl Fred Wilma Pebbles Dino Barney Betty Bamm-Bamm 23:18 >
Please note the line:
use strict;
Get in the habit of including this in all your scripts, and it will save you a lot of debugging time in the long run. (Essentially, it just means you will need to put my before each variable the first time that variable is used in the script.)
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|