Jeff Westman wrote:

On 6/20/05, John W. Krahn <[EMAIL PROTECTED]> wrote:

It looks like you don't really need to use paragraph mode, this should do what
you want:

#!/usr/bin/perl
#
# tnsnames.pl -- reads tnsnames.ora, sorts by host name
use warnings;
use strict;

my $tnsFile = 'tnsnames.ora';
open F, '<', $tnsFile or die "Could not open file $tnsFile: $!\n";

my ( $host, $name, %tns ) = ( '', '' );
while ( <F> ) {
   $host = $1 if /\(HOST\s*=\s*(\w+)\)/;
   $name = $1 if /\(SERVICE_NAME\s*=\s*(\w+)\)/;
   if ( length $host and length $name ) {
       print "*** RESULT : host= $host, service= $name\n";
       $tns{ $host } = $name;
       ( $host, $name ) = ( '', '' );
       }
   }
close F;

for ( sort keys %tns ) {
   printf "%-8s = %s\n", $_, $tns{ $_ };
   }

__END__

Thank you for your post.  I know I could have just done some
"top-down" scanning for pairs (I like your style, BTW), but it seemed
safer to do multi-line matching, so I am still a bit bothered why I
couldn't get the match to work in all cases (ie, a blank line was
mandatory).

I also had a basic question on the code you sent.  You have:

     $host = $1 if /\(HOST\s*=\s*(\w+)\)/;
     $name = $1 if /\(SERVICE_NAME\s*=\s*(\w+)\)/;

This works and makes sense. But what if the line is "commented"?! That is, when I modified this and made this:

     $host = $1 if /[^#].*\(HOST\s*=\s*(\w+)\)/;
     $name = $1 if /[^#].*\(SERVICE_NAME\s*=\s*(\w+)\)/;

it SHOULD have worked (I think!).  It still matched a line that should
have been ignored.  What am I doing wrong?!

The problem is that [^#] can match anywhere so in the string
"ab#cd(HOST = xyz)" it can match at 'a' or 'b' or 'c' or 'd'.

This should work better:

my $para = '';

while ( <F> ) {

    $para  .= $_;

    next if $para =~ tr/(// != $para =~ tr/)//;

    $para =~ s/#.*//g;  # remove comment lines

    if ( $para =~ /\(HOST\s*=\s*(\w+)\).*?\(SERVICE_NAME\s*=\s*(\w+)\)/s ) {

        print "*** RESULT : host= $1, service= $2\n";

        $tns{ $1 } = $2;
        }

    $para = '';
    }


for ( sort keys %tns ) {
   printf "%-8s = %s\n", $_, $tns{ $_ };
   }



John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to