in reply to Am I doing Greedy Matching?

You say the data looks like machine1-basement.xyz.com but the pattern /[\w]*\-[\w]*/ (or more simply /\w*-\w*/) will only match the string machine1-basement, not the whole domain name.

You probably need something like this:

open SOURCETNS, '<', "/home/$User/Work/PROJ/$sourceTnsFileName" or die + "Cannot open '/home/$User/Work/PROJ/$sourceTnsFileName' because: $!" +; while ( my $record = <SOURCETNS> ) { push @sourceDBHostsFromTnsEntry, $record =~ /HOST=(\S+-\S+)/g; push @sourceDBPortsFromTnsEntry, $record =~ /PORT=(\d+)/g; } close SOURCETNS;

Replies are listed 'Best First'.
Re^2: Am I doing Greedy Matching?
by vishi (Beadle) on Nov 10, 2011 at 11:39 UTC

    I do not want the domain name.. I just need the machine1-basement string in my array and hence the Regex. I just discovered the "/g" and will try it out and let you know.. Thanks !

      OK, so change:

      push @sourceDBHostsFromTnsEntry, $record =~ /HOST=(\S+-\S+)/g;

      To:

      push @sourceDBHostsFromTnsEntry, $record =~ /HOST=(\w+-\w+)/g;
        Ah Perfect! I think I had missed the /g and hence the array with only one record. What you suggested worked perfectly! Thanks!

        I tried out the other responses as well.. they work too. I liked the simplicity of the foreach method - easy to understand, but its kinda long to type. I keep very descriptive variable names so as to make the code (variables and their purpose) easy to understand by another person who;s looking at my code. In fact, it helps me too, if I have to revisit my code after a year or two.

        Thanks all for your suggestions! Truly and highly appreciate your contributions :-)