Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

You can't use foreach like this - you need to use while to make your code work. See the code below for a demo:

$data = <<'DATA'; foo 1.1.1.1 foo bar 22.22.22.22 bar baz 333.333.333.333 baz DATA # this is *wrong* foreach($data=~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/g){ push @ips, $1; } print "Foreach fails and gives:\n"; print "$_\n" for @ips; @ips = (); # this is right while($data=~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/g){ push @ips, $1; } print "While works and gives:\n"; print "$_\n" for @ips;

For an explanation of why the for loop generates three copies of the last IP address you need to consider what has happened. A /g type regex will return an array of matches. So as it is called in array context it returns an array (this actually contains the three addresses) however $1 now contains the last address matched as we had to match all occurences to generate the array. We then iterate over this array and push $1 (the last IP address) into our array three times. To fix this we either use while or push $_ (not $1) into our IP array like this:

$data = <<'DATA'; foo 1.1.1.1 foo bar 22.22.22.22 bar baz 333.333.333.333 baz DATA foreach($data=~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/g){ push @ips, $_; } print "Foreach now gives:\n"; print "$_\n" for @ips;

Oh you can slurp up the file into a variable $data like this:

open FILE, "<path/to/file" or die "Oops, Perl says: $!"; { local $/; $data = <FILE>; } close FILE;

cheers

tachyon

s&&rsenoyhcatreve&&&s&n\w+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print


In reply to Re: Re: Extracting IP addresses from a file... by tachyon
in thread Extracting IP addresses from a file... by Brian Matchick

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 meditating upon the Monastery: (5)
As of 2024-04-19 01:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found