SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2004
    Location
    USA
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    replace date with string

    Hi,

    I am new to perl. I have file with sql statements. I want to replace date (in format - 2008-06-06) with string "now()". How do I do this? I tried -
    Code:
    s/$dt/now()/
    doesn't work.

    thanks,

  2. #2
    SitePoint Addict
    Join Date
    Apr 2006
    Posts
    249
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It'd help to see more of your script, but it should probably look something like:

    $sql =~ s/$dt/now()/g;

  3. #3
    SitePoint Zealot
    Join Date
    Sep 2004
    Location
    USA
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well. I don't have entire script ready. But, this is my test script -
    Code:
    print 'Enter value for Date and Time : ';
    chomp($_ = <STDIN>);
    $dt = $_;
    $rep = "now()";
    if ($dt =~ m/^\d{4}-\d{2}-\d{2}$/) {
    	print 'Valid Date',$dt,"\n";
    	$newvalue = s/\$dt/\$rep/g;
    	print 'New Value : ', $newvalue;
    } else {
    	print 'Invalid Date';
    }

  4. #4
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why are you escaping the "$" symbol in the regexp?, that kills the variable interpolation.

    $newvalue = s/\$dt/\$rep/g;

    try:

    $newvalue = s/$dt/$rep/g;

  5. #5
    SitePoint Zealot
    Join Date
    Sep 2004
    Location
    USA
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no luck

  6. #6
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I missed that you are using a different variable ($newvalue) and using "=" when you should use "=~":

    Code:
    $dt = '2008-12-25';
    $rep = "now()";
    if ($dt =~ m/^\d{4}-\d{2}-\d{2}$/) {
    	print 'Valid Date',$dt,"\n";
    	($newvalue = $dt) =~ s/$dt/$rep/g;
    	print 'New Value : ', $newvalue;
    } else {
    	print 'Invalid Date';
    }
    or just modify $dt:

    Code:
    $dt = '2008-12-25';
    $rep = "now()";
    if ($dt =~ m/^\d{4}-\d{2}-\d{2}$/) {
    	print 'Valid Date',$dt,"\n";
    	$dt =~ s/$dt/$rep/g;
    	print 'New Value : ', $dt;
    } else {
    	print 'Invalid Date';
    }

  7. #7
    SitePoint Zealot
    Join Date
    Sep 2004
    Location
    USA
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •