Perl Monk, Perl Meditation | |
PerlMonks |
Re: Re: Extracting IP addresses from a file...by tachyon (Chancellor) |
on Jul 05, 2001 at 00:02 UTC ( [id://93928]=note: 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:
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:
Oh you can slurp up the file into a variable $data like this:
cheers tachyon s&&rsenoyhcatreve&&&s&n\w+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
In Section
Seekers of Perl Wisdom
|
|