Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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; }

HTH

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


In reply to Re: Trouble with foreach loop.... I think by arturo
in thread Trouble with foreach loop.... I think by u235sentinel

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-25 16:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found