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

File_Names:-
VMTKSWPDCN0120080717.TXT
VMTKSWPSLN0120080717.TXT
VMTKSWPDLA0120080717.TXT
CMVMCSHFEN0120080605.TXT
VMTKSWPDLN0120080605.TXT
VMTKSWPTVN0120080605.TXT
VMTKSWPSLN0120080605.TXT
VMTKSWPDLA0120080605.TXT
VMTKSWPTVA0120080605.TXT
VMTKSWPDCN0120080605.TXT
VMTKSWPSLA0120080605.TXT
$source = 'VM'; $COB = '20080605'; if ( /^$source.*$COB/ ) { .... do something ... }
When I take the patter in a varibale to match the file it does not take any effect at all.

Replies are listed 'Best First'.
Re: Passing regex inside via a variavble
by ELISHEVA (Prior) on Mar 10, 2009 at 06:08 UTC

    The problem isn't in the regular expression, but what it is matching.

    if ( /^$source.*$COB/ ) matches the regular expression against the variable $_: if your file name is stored in some other variable, that condition won't work. If you think your file name is being stored in $_, you'll have to include some code showing how you are setting the value of $_ for us to understand why that isn't working.

    Alternatively, if the lines you are reading in get stored in a variable $foo then you could modify the condition to check the regular expression against the variable $foo, like this: if ( $foo =~ /^$source.*$COB/ ).

    Best, beth

Re: Passing regex inside via a variavble
by CountZero (Bishop) on Mar 10, 2009 at 06:09 UTC
    It works perfectly for me:
    use strict; my $source = 'VM'; my $COB = '20080605'; while (<DATA>) { if (/^$source.*$COB/) { print "Matched $_"; } ## end if (/^$source.*$COB/) } ## end while (<DATA>) __DATA__ VMTKSWPDCN0120080717.TXT VMTKSWPSLN0120080717.TXT VMTKSWPDLA0120080717.TXT CMVMCSHFEN0120080605.TXT VMTKSWPDLN0120080605.TXT VMTKSWPTVN0120080605.TXT VMTKSWPSLN0120080605.TXT VMTKSWPDLA0120080605.TXT VMTKSWPTVA0120080605.TXT VMTKSWPDCN0120080605.TXT VMTKSWPSLA0120080605.TXT
    Output:
    Matched VMTKSWPDLN0120080605.TXT Matched VMTKSWPTVN0120080605.TXT Matched VMTKSWPSLN0120080605.TXT Matched VMTKSWPDLA0120080605.TXT Matched VMTKSWPTVA0120080605.TXT Matched VMTKSWPDCN0120080605.TXT Matched VMTKSWPSLA0120080605.TXT
    Are you sure that $_ contains the correct string to match?

    Note: if you have to match against a huge number of strings, it will be faster to pre-compile the regex-pattern:

    my $source = 'VM'; my $COB = '20080605'; my $regex = qr /^$source.*$COB/; while (<DATA>) { if (/$regex/) { print "Matched $_"; } }

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Passing regex inside via a variavble
by ikegami (Patriarch) on Mar 10, 2009 at 06:12 UTC

    Works for me.

    $source = 'VM'; $COB = '20080605'; for (qw( VMTKSWPDCN0120080717.TXT VMTKSWPSLN0120080717.TXT VMTKSWPDLA0120080717.TXT CMVMCSHFEN0120080605.TXT VMTKSWPDLN0120080605.TXT VMTKSWPTVN0120080605.TXT VMTKSWPSLN0120080605.TXT VMTKSWPDLA0120080605.TXT VMTKSWPTVA0120080605.TXT VMTKSWPDCN0120080605.TXT VMTKSWPSLA0120080605.TXT )) { if ( /^$source.*$COB/ ) { print("$_: match\n"); } else { print("$_: no match\n"); } }
    VMTKSWPDCN0120080717.TXT: no match VMTKSWPSLN0120080717.TXT: no match VMTKSWPDLA0120080717.TXT: no match CMVMCSHFEN0120080605.TXT: no match VMTKSWPDLN0120080605.TXT: match VMTKSWPTVN0120080605.TXT: match VMTKSWPSLN0120080605.TXT: match VMTKSWPDLA0120080605.TXT: match VMTKSWPTVA0120080605.TXT: match VMTKSWPDCN0120080605.TXT: match VMTKSWPSLA0120080605.TXT: match

    Make sure your variables ($_, $source and $COB) contain what you think they do. Specifically, look for trailing newlines that need to be chomped.