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

Revered Monks,
I have some variables read in from a user defined perl-based config file. Example of a typical name value pair is:

$filename= YYYYMMDD_something.xyz

Now, I am trying substitute the YYYYMMDD in the above variable with a date which is present in another variable $DATE.
To do this I am doing:

$filename =~ s/YYYYMMDD/$DATE/i;

The problem is after this regex the value of $filename becomes:

20061124_somethingxyz

But I the value to be:

20061124_something.xyz

Any advice on how I can go about this is highly appreicated.

Thanks!

Replies are listed 'Best First'.
Re: Quick question on regex
by ikegami (Patriarch) on Nov 24, 2006 at 17:40 UTC
    Your problem is elsewhere. The . was not removed by that regexp. Or maybe you're not using strict (bad!) and really did forget to put the quotes around YYYYMMDD_something.xyz.
    my $filename = 'YYYYMMDD_something.xyz'; my $DATE = '20061124'; $filename =~ s/YYYYMMDD/$DATE/i; print($filename, "\n"); # 20061124_something.xyz

      ...and yet another v. good reason to always use strict. I wouldn't normally mention it, but it seems a bit more insidious than the normal misspelt variable...

      BTW OP, you've probably realised, but, in that context, the "." is interpreted as a string concatenation character.

      Tom Melly, tom@tomandlu.co.uk
      correct thanks!