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!

In reply to Am I doing Greedy Matching? by vishi

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.