Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: Trouble with foreach loop.... I think

by arturo (Vicar)
on May 29, 2004 at 03:53 UTC ( #357416=note: print w/replies, xml ) Need Help??

in reply to Trouble with foreach loop.... I think

In a way, your code is doing exactly what you say you want it to: it is indeed matching every line in @netstat that matches [your] @matches list", but the way perl interprets that probably doesn't capture what you're thinking, What your code does is give you every line in the netstat output that matches *your entire @matches array*, interpreting that array as a string. The matching operator is a double-quote context (just like print "@matches"). And, as it says in perldata:
Arrays and slices are interpolated into double-quoted strings by joining the elements with the delimiter specified in the $" variable($LIST_SEPARATOR if "use English;" is specified), space by default.
In other words, the line:
my @results = grep( /@matches/, @netstat );
comes out (given the rest of your code) as equivalent to my @results = grep(/invalid headers packets dropped/, @netstat);
in other words it's trying to find that whole "phrase."

I think you also mixed up your foreach loop, which loops over @netstat, when I think what you wanted was to check each element in @matches; putting all that together, I think your loop should be:

foreach my $match (@matches) { my @results = grep /$match/, @results; print @results; }


If not P, what? Q maybe?
"Sidney Morgenbesser"

Replies are listed 'Best First'.
Re: Re: Trouble with foreach loop.... I think
by u235sentinel (Hermit) on Jun 01, 2004 at 16:03 UTC
    I see the confusion in my code now. I modified it and all is well. I must have read through that section in "Learning Perl 3rd Edition" a dozen times and still didn't get it :-)

    Thanks for the help!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://357416]
[robby_dobby]: LanX: But that's in Africa! More like YAPC::Africa
choroba fears Amsterdam might fill him with enthusiasm to do nothing
[robby_dobby]: Uff, too expensive :-(
[LanX]: ... I found YAPC EU more entertaining (and international )than YAPC NA
[robby_dobby]: What's the site for YAPC::EU?
[LanX]: Robby Dobby no IIRC Sinai is already part of Asia
[robby_dobby]: page for yapceu is out of date by 2 years
[robby_dobby]: LanX: Sharm-el-Sheikh is in Egypt
[robby_dobby]: Thanks, choroba

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (16)
As of 2017-04-24 15:59 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (442 votes). Check out past polls.