in reply to Test RegEx

I was trying to understand your code and I just got lost!
Can you provide just 2 input files and desired output?

Here is where I got to before being completely lost.

#!/usr/bin/perl use strict; # turned on strict and warnings. use warnings; # a "#" in front makes them comments my $DFile="/home/infosec/data/RegEx"; open (DATAF, "<$DFile") || die ("Cannot open DATAF file \n"); # don't use DATA as a file handle, that is a reserved word for Perl. # no need to "eval" $regex foreach my $line (<DATAF>) { chomp $line; my ($tname, tvalu, $tfile, $regex) = split(/:/, $line); open (TFILE, "<$tname") || die ("Cannot open Test file $tname\n"); while (<TFILE>) { next unless /$regex/; print $_; # this is so obtuse that my brain hurts! # These super tricky Perl Special Variables like # $& $- $+ are EXTREMELY seldom needed. Get rid of them. # please explain what this is supposed to do.... # $- is a scalar, not an array, what do you intend by $-[0]? # # print "\t\t\$&: ", # substr( $_, $-[0], $+[0] - $-[0] ), # "\n"; # foreach my $i ( 1 .. $#- ) # { # print "\t\t\$$i: ", # substr( $_, $-[$i], $+[$i] - $-[$i] ), # "\n"; # } } print "\n"; } print "\n";

Replies are listed 'Best First'.
Re^2: Test RegEx
by AnomalousMonk (Archbishop) on Apr 22, 2010 at 23:11 UTC

    I, too, got lost and gave up pretty quickly (although I think JavaFan has set the OPer on the right path), but  @- and  @+ are regex-related special variables, so things like  $-[0] and  $+[0] are valid.

    Another oddity of the OPer's original code is

    my @LINES=<DATA>; my $tLINES=@LINES; foreach $RECORD (@LINES[0..$tLINES-1]) { ... }

    This seems to boil down to (assuming you don't just use a  while loop)

    my @LINES = <PROPERFILEHANDLE>; foreach $RECORD (@LINES) { ... }