vishi has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks!
I am trying to match all occurances of a particular pattern in a file and put all the matched patterns into an array. I have the code below, but I'm not sure why it's not working.
The file has about 2-3 lines and the pattern I am looking for is like this : HOST=<something>-<domain.com> PORT=<num>, HOST=<something1>-<domain.com> PORT=<num1> ....etc
It looks something like this:
HOST=machine1-basement.xyz.com PORT=1234 HOST=machine2-attic.xyz.com PORT=9999 HOST=machine3-garage.xyz.com PORT=5555
I want to put all hosts in one array and the ports in another array. So, I am opening this file, reading one line at a time and pushing all matches into an array. However, this is not working!
open (SOURCETNS, "/home/$User/Work/PROJ/$sourceTnsFileName"); while ($record = <SOURCETNS>) { chomp ($record); if ($record =~ /[\w]*\-[\w]*/) { push (@sourceDBHostsFromTnsEntry, $&); } if ($record =~ /[\d]*/) { push (@sourceDBPortsFromTnsEntry, $&); } } close(SOURCETNS);
What am I doing wrong? Is this something like greedy matching? The problem here is that these patterns all occur in the same line...so I need to know how I should match multiple patterns in the same line and push them into the array?
Thanks!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Am I doing Greedy Matching?
by jwkrahn (Abbot) on Nov 10, 2011 at 11:34 UTC | |
by vishi (Beadle) on Nov 10, 2011 at 11:39 UTC | |
by jwkrahn (Abbot) on Nov 10, 2011 at 11:43 UTC | |
by vishi (Beadle) on Nov 10, 2011 at 12:33 UTC | |
|
Re: Am I doing Greedy Matching?
by cavac (Prior) on Nov 10, 2011 at 11:53 UTC | |
by jwkrahn (Abbot) on Nov 10, 2011 at 12:06 UTC | |
by cavac (Prior) on Nov 10, 2011 at 21:56 UTC | |
|
Re: Am I doing Greedy Matching?
by reisinge (Hermit) on Nov 10, 2011 at 12:45 UTC |