Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: regex match with interpolated pattern

by drock (Beadle)
on Aug 18, 2004 at 15:45 UTC ( #384007=note: print w/replies, xml ) Need Help??


in reply to Re: regex match with interpolated pattern
in thread regex match with interpolated pattern

ok men, I tried all 3 suggestions and none worked so here is my code. Again $dm and $dmm are dereived from the subroutines are are in format xx/xx/xx
sub date_manip { my ($month, $day, $year) = (localtime)[4,3,5]; sprintf ("%02d/%02d/%02d\n", $month+1,$day,($year % 10 +0)); } my $dm = '&date_manip'; sub date_manip1 { my $days = (shift); my ($d, $m, $y) = (localtime($time - $days * 86400)) [ +3..5]; sprintf ("%02d/%02d/%02d", $m+1,$d,($y % 100)); } my $dmm = '&date_manip1(1)'; #print ("$dm $dmm"); ############################################################ ## Gather data for scratch tapes; open file, create list, then print i +t. ## ############################################################ open (FOO, ">$scratches") || die "could not open file:$!"; #($^I, @ARGV) = ('.bak', $scratches); open (D, "$logf") || die "could not open file:$!"; while (<D>) { ## look for 9840S and ebexpire ## declare OFS = tab ## tell split to split on IRS 0,1&5. # if (($_ =~ /9840S/) && ($_ =~ /ebexpire, ebexpire/ ) +) { if (($_ =~ /9840S/) && ($_ =~ /ebexpire, ebexpire/ ) & +& ( $_ =~ m/$dm/) && ($_ =~ m/$dmm/)) { local $, = "\t"; print FOO +(split)[0,1,5], $/ ; } } close (FOO); close (D);

Replies are listed 'Best First'.
Re^3: regex match with interpolated pattern
by Roy Johnson (Monsignor) on Aug 18, 2004 at 16:21 UTC
    This code works (that is, the print executes) for me, and should work for you:
    $dm='08/14/04'; $_='There is an 08/14/04 in here'; if ($_ =~ m/$dm/) { print "Matched on <$_>\n"; }
    The code you've included so far hasn't been the kind of test code we'd most like to see, which is an actual program that we can run that demonstrates the problem.

    In this last excerpt, you're quoting your subroutine calls, which results in the subroutines not being called. Also, you're putting a newline in your date_manip, which may not be what you want. Here's a test-code modification that may help you out:

    #!perl sub date_manip { my ($month, $day, $year) = (localtime)[4,3,5]; sprintf ("%02d/%02d/%02d\n", $month+1,$day,($year % 100)); } my $dm = &date_manip; sub date_manip1 { my $days = (shift); my ($d, $m, $y) = (localtime($time - $days * 86400)) [3..5]; sprintf ("%02d/%02d/%02d", $m+1,$d,($y % 100)); } my $dmm = &date_manip1(1); print ("{$dm} {$dmm}\n"); ############################################################ ## Gather data for scratch tapes; open file, create list, then print i +t. ## ############################################################ while (<DATA>) { print "-- Checking <$_>\n"; print "Found 9840\n" if /9840S/; print "Found ebexpire\n" if /ebexpire, ebexpire/; print "Found $dm\n" if /$dm/; print "Found $dmm\n" if /$dmm/; } __DATA__ 9840S ebexpire, ebexpire 12/30/69 08/18/04 9840S ebexpire, ebeXpire 12/30/69 08/18/04 9840S ebexpire, ebexpire XX/30/69 08/18/04

    Caution: Contents may have been coded under pressure.
Re^3: regex match with interpolated pattern
by Zed_Lopez (Chaplain) on Aug 18, 2004 at 16:27 UTC

    Uncomment your print ("$dm $dmm");. You'll see that you actually have the literal strings '&date_manip' and '&date_manip1(1)' in them. That's why they're not matching something that looks like a date.

    Just use:

    $dm = date_manip(); $dmm = date_manip1(1);

    And you'll be a lot closer.

    You also may wish to check out Date::Calc and Date::Manip and choose one for your date routines.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://384007]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2023-02-04 02:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (30 votes). Check out past polls.

    Notices?