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

Hello Monks! This snippet of a larger cgi script that removes a table entry in it's last phase. I first attempted doing this using a while loop inside the flock mechanizm. I read in the camel book (page 154)s///, arrays and while loops dont mix well. But it still looks......well doable.........
The value of $RTBL_ENTRY_DEL is:'g2222222|mvs+VSPMVSPNODE' s/$RTBL_ENTRY_DEL//; But only the g2222222 is removed leaving the "|MVS+VSPMVSPNODE". I tri +ed negation of the pipe in the same sustitution line with no success.
So I tried something totaly different.....
#!/usr/local/bin/perl -w + use strict; + use Fcntl qw(:flock); + #-------------------- + my $CE_PMID = 'g2222222' ; + my $RTBL_CD_NODE_CONST = '|mvs+VSPMVSPNODE' ; + my $CE_ROUTE_TABLE = "/edi/cdunix/ndm/bin/Rout.Tbl"; + my $RTBL_ENTRY_DEL = $CE_PMID . $RTBL_CD_NODE_CONST; + #-------------------- if ( $CE_PMID =~ /g*/ ) { + open(CE_RTBL, "<$CE_ROUTE_TABLE") || die "Couldnt read from routing ta +ble: $!"; my @RTBL_Array = <CE_RTBL>; + close(CE_RTBL) || die " Couldnt close file: $!"; + my @RTBL_Match = grep { /$RTBL_ENTRY_DEL/ } @RTBL_Array; + if ( @RTBL_Match ) { + print "It matched!!!\n"; + open(CE_RTBL,"+<$CE_ROUTE_TABLE"); fl +ock(CE_RTBL, 2) or die "Can't file lock $CE_ROUTE_TABLE: $!"; + print "Inside flock:$RTBL_ENTRY_DEL\n"; + for (@RTBL_Array) {s/$RTBL_ENTRY_DEL//g}; + close (CE_RTBL) || die "Can't close $CE_ROUTE_TABLE +: $!"; } #End of if + } #End of if
It didn't work either...... Here's the table that I'm attempting to remove an entry from each has a new line terminator:
o2130338|mvs+VSPMVSPNODE g2139819|mvs+VSPMVSPNODE g2113717|mvs+VSPMVSPNODE o2156228|mvs+VSPMVSPNODE o2170511|mvs+VSPMVSPNODE o2174150|mvs+VSPMVSPNODE o2194340|mvs+VSPMVSPNODE o4199702|mvs+VSPMVSPNODE g2345678|mvs+VSPMVSPNODE g2222222|mvs+VSPMVSPNODE
If anyone would be kind enough to point me in the right direction I'd be very greatful.

Replies are listed 'Best First'.
Re: regex problem? deleting table entry
by zer (Deacon) on Mar 30, 2006 at 00:20 UTC
    #!/usr/bin/perl use strict; use warnings; use Fcntl qw(:flock); + my $CE_PMID = 'g2222222' ; my $RTBL_CD_NODE_CONST = '\|mvs\+VSPMVSPNODE' ; my $CE_ROUTE_TABLE = "/edi/cdunix/ndm/bin/Rout.Tbl"; my $RTBL_ENTRY_DEL = $CE_PMID . $RTBL_CD_NODE_CONST; + for (<DATA>) {s/$RTBL_ENTRY_DEL//g;print}; + __DATA__ o2130338|mvs+VSPMVSPNODE g2139819|mvs+VSPMVSPNODE g2113717|mvs+VSPMVSPNODE o2156228|mvs+VSPMVSPNODE o2170511|mvs+VSPMVSPNODE o2174150|mvs+VSPMVSPNODE o2194340|mvs+VSPMVSPNODE o4199702|mvs+VSPMVSPNODE g2345678|mvs+VSPMVSPNODE g2222222|mvs+VSPMVSPNODE

    The problem is that regex interprets your variables as regex not as a value.
    g2222222\|mvs\+VSPMVSPNODE <--what it should be.

Re: regex problem? deleting table entry
by duff (Parson) on Mar 30, 2006 at 04:07 UTC

    It is as zer says. There are characters in your variable that are special to the regular expression engine (namely | and +). An alternative to manually back-whacking them would be to use quotemeta or the \Q "operator" within the regular expression. Here are two examples that work the same ...

    Example 1:

    $RTBL_ENTRY_DEL = quotemeta $RTBL_ENTRY_DEL; # ... and then later ... s/$RTBL_ENTRY_DEL//g;
    Example 2:
    s/\Q$RTBL_ENTRY_DEL//g;
    I prefer the latter for what I believe are obvious reasons :-)
      That got me going....Thanks for the Help!