in reply to Am I doing Greedy Matching?

As far as i can see, yes you are greedy-matching.

Before i start: You don't seem to use strict; and use warnings;, neither the 3-argument version of open, nor checking if open actually worked. Filehandles should also be written as scalars ("$hello") - which you would have noticed when using strict.

I'm a bit oldstyle, i would do it like this:

my @sourceDBHostsFromTnsEntry; my @sourceDBPortsFromTnsEntry; open(my $SOURCETNS, "<", "/home/$User/Work/PROJ/$sourceTnsFileName") o +r die($!); while((my $record = <$SOURCETNS>)) { chomp ($record); my @parts = split /\ /, $record; foreach my $part (@parts) { if($part =~ /HOST=(.*)/o) { push @sourceDBHostsFromTnsEntry, $1; } if($part =~ /PORT=(.*)/o) { push @sourceDBPortsFromTnsEntry, $1; } } } close($SOURCETNS);

If i wrote the program, i also would have decided to use shorter variable names (e.g. names that could be keyed in by remembering them instead of having to do copy&paste ;-)

I didn't test this specific code and there are probably "nicer" ways to do it but it should get the job done (although there may be the odd typo).

Explanation: In the first step, i read in a line and chop it up on the "space" delimeter into @parts. Then, foreach $part in @parts i match against the two tags "HOST=" and "PORT=". If it matches, i put the remainder into their respective arrays.

Don't use '#ff0000':
use Acme::AutoColor; my $redcolor = RED();
All colors subject to change without notice.

Replies are listed 'Best First'.
Re^2: Am I doing Greedy Matching?
by jwkrahn (Abbot) on Nov 10, 2011 at 12:06 UTC
    In the first step, i read in a line and chop it up on the "space" delimeter into @parts.

    You also chomp the line which isn't required because (.*) will not match a newline.

    And you are using the /o option which is superfluous because there are no variables in the pattern to interpolate.

      You also chomp the line which isn't required

      Quite right, it isn't required (and could hurt performance slightly on a big file). Over time, it has become part of my coding style to always use chomp when reading from a filehandle. More than once did i change the regex or some other part of the code and suddenly wondered where the newline came from. So i started using chomp unless required otherwise.

      And you are using the /o option which is superfluous because there are no variables in the pattern to interpolate.

      Again you are quite right. Does this actually hurt performance? I find it rather helpful when coding, even in static expressions, since i could just glance at the end and see "ok, this isn't a dynamic regex" instead of looking through the whole line noise for variables.

      But thats probably just me. You know, old dogs, new tricks...

      Don't use '#ff0000':
      use Acme::AutoColor; my $redcolor = RED();
      All colors subject to change without notice.