lanier has asked for the wisdom of the Perl Monks concerning the following question:

Here is my updated code.
#! /u/ss/bin/perl -w $pattern=qr/ ( ^[^=|#]*=\s*\(DESCRIPTION\s*=\s* \(ADDRESS_LIST\s*=\s*\(ADDRESS\s*=\s* \(COMMUNITY\s*=\s*[^)]*\)\s*\(PROTOCOL\s*=\s*TCP\)\s* \(HOST\s*=\s*[^)]*\)\s*\(PORT\s*=\s*\d+\)\s*\)\s*\)\s* \(CONNECT_DATA\s*=\s*\(SID\s*=\s*[^)]*\)\s*\)\s* \)\s* ) /x; open FILE,"tnsnames.ora" or die $!; {local $/=undef; $_ = <FILE>; } close FILE; print "This is tnsnames.ora\n"; @sections=/$pattern/g; foreach $el (@sections) { print $el; } @sorted=sort @sections; open FILE,"tns.log" or die $!; {local $/=undef; $_ = <FILE>; } close FILE; @sections=/$pattern/g; print "This is tns.log\n"; foreach $el (@sections) { print $el; } $x=pop @sorted || ''; print "This is $x\n"; $y=pop @sections || ''; print "This is y: $y\n"; while( $x || $y ){ if( $x gt $y ){ #print "missing from file1: $x\n"; $x = pop @sorted || ''; }elsif( $y gt $x ){ #print "missing from file2: $y\n"; $y = pop @sections || ''; }else{ $x = pop @sorted || ''; $y = pop @sections || ''; } }
When I first try to print the @sections array for the first file there is nothing. when i print it for the 2nd file i get output. I am wondering if my pattern not matching the 1st file but somehow matching the 2nd file.
example for first file: ############### # Filename......: tnsnames.ora # Name..........: LOCAL_REGION.lanier.com # Date..........: 24-OCT-95 14:30:07 ################ ##### TF #####Oracle Application File Server FNDFS_tf.lanier.com = (DESCRIPTION = (ADDRESS = (COMMUNITY = tcp.lanier.com) (PROTOCOL = tcp ) (HOST = tf.lanier.com) (PORT = 1521) ) (CONNECT_DATA = ( SID = FNDFS ) ) ) sdbet.lanier.com = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (COMMUNITY = tcp.lanier.com) (PROTOCOL = TCP) (Host = sb.lanier.com) (Port = 1521) ) ) (CONNECT_DATA = (SID = sdbet) (GLOBAL_NAME = sdbet.lanier.com) ) )
I am trying to ignore the lines that start with # in my pattern
example of 2nd file: acp.lanier.com = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (COMMUNITY = tcp.lanier.com) (PROTOCOL = TCP) (HOST = st.lanier.com) (PORT = 1533) ) ) (CONNECT_DATA = (SID = utl) ) ) acpd.lanier.com = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (COMMUNITY = tcp.lanier.com) (PROTOCOL = TCP) (HOST = sp.lanier.com) (PORT = 1531) ) ) (CONNECT_DATA = (SID = utld) ) )

Edit by tye, title, add CODE tags around data

Replies are listed 'Best First'.
Re: Regular Expressions to ignore lines starting with #
by Mr. Muskrat (Canon) on May 19, 2003 at 18:34 UTC

    Instead of using:

    open FILE,"tnsnames.ora" or die $!; {local $/=undef; $_ = <FILE>; } close FILE;

    I'd probably use something like this:
    open(FILE, '<', 'tnsnames.ora') or die "Cannot open tnsnames.ora for reading, stopped ",$!; my $tnsnames = ''; for (<FILE>) { next if /^#/; $tnsnames .= $_; } close FILE;

    It will skip the lines beginning with # and add all the rest into the scalar $tnsnames.

      next if /^#/;

      I'd likely do it similarly but I'd change the regex a bit. Using /^\s*#/ will skip lines where the first non-whitespace character is an octothorpe, not just the first character. That's usually how comments are defined.

      -sauoq
      "My two cents aren't worth a dime.";
      

        and i would do it like this:

        s/#.*$//; next if /^$/;

        to remove any comment (up to end of line) and skip if there's nothing left. but this might be stretching the requirement a bit.

        I use to do it like:
        next if /^\s*(#.*)?$/;
        skipping "empty" lines too, which of course will fail if you use HERE document like constructs.