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,


In reply to Re: Looping in a Loop by Athanasius
in thread Looping in a Loop by Violet

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.