CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Flummoxed by variable in regexp

 



MorayJ
Novice

Oct 4, 2011, 4:43 AM

Post #1 of 3 (4420 views)
Flummoxed by variable in regexp Can't Post

I have just spent ages trying to get a variable to work as the substitution part of a regexp. It continued to contain the first assignation to the variable, and not subsequent changes.

Couldn't understand why I couldn't find a comprehensible answer - nearest was finding /ee and eval blocks which, to be honest, I was not really understanding.

Anyway, had got in the habit of just trying to change something in the line to see if it would work.

So my substitution looked like this:


Code
$line =~ s/DATE/$newdate/;


This resulted in $newline only ever concerning its first assignation - in effect, it became static.

I had the line:

Code
print "$newdate\n";


following on from it, and this was updated with the new date. Frustration, bitterness and incomprehension followed.

I then thought I would post the problem and decided to tidy it up so it was doing exactly what I wanted.


Code
$line = s/\(\d.*?\)/\($newdate\)/;


And suddenly $newdate in the substitution is updating and working fine.

Is there anyone who could explain this to me?

Thanks
Moray


BillKSmith
Veteran

Oct 5, 2011, 9:17 PM

Post #2 of 3 (4300 views)
Re: [MorayJ] Flummoxed by variable in regexp [In reply to] Can't Post

Could you please post a small program that exhibits the problem. We cannot comment on code that we have not seen. The following code does everything that you described. It works exactly as desired.


Code
  

use strict;
use warnings;

my $line1 = 'Some text (1 Oct 2010) is clear (11 Sept 1822).';
my $newdate = '2 Oct 2011';
foreach (1..2){
my $line = $line1;
$line =~ s/\(\d.*?\)/($newdate)/g;
print $newdate, "\n";
print $line, "\n";
$newdate = '3 Oct 2011';
}

Good Luck,
Bill


MorayJ
Novice

Oct 6, 2011, 3:41 PM

Post #3 of 3 (4199 views)
Re: [BillKSmith] Flummoxed by variable in regexp [In reply to] Can't Post

Hello,

Thank you for taking the time to compose a reply. You had pretty much recreated the situation.

I was building up my response with more detail and changing your example, when it hit me that I am a complete idiot.

I was iterating over a line and whilst testing it hadn't bothered to use the actual brackets with the date in regex.

Instead, I was just changing a word - a word that was no longer there to be changed on the next iteration and hence the line remaining with only the first change of date in it.

This then being my mistaken assumption that the variable wasn't changing - it was, but the substitution was now failing.

Apologies and thank you for your help with this as it was driving me mad and you helped me focus on it.

Cheers
Moray

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives