in reply to Re^2: trying to do a simple search
in thread trying to do a simple search
You are still reading your data one line at a time so your pattern matches are matching against one line at a time.
To see what is being read, you might experiment with the following program:
#!/usr/bin/perl -w use strict; use warnings; while (<DATA>) { print "start of loop\n"; print "\$_ = \"$_\"\n"; } __DATA__ <a>a, b</a> <b>a,b</b> <b>a,b </b>
To make <DATA> read all your DATA instead of just one line, you can set $/ before your while loop, as follows. Note the use of a block ({ }) and local so that $/ isn't affected elsewhere in the program.
#!/usr/bin/perl -w use strict; use warnings; { local $/; while (<DATA>) { print "start of loop\n"; print "\$_ = \"$_\"\n"; } } __DATA__ <a>a, b</a> <b>a,b</b> <b>a,b </b>
With $/ set to undef (which is what local $/ does), there is no need for the while loop - all the data is read on the first iteration. You can read all the data into a single variable as follows:
#!/usr/bin/perl -w use strict; use warnings; my $var = do { local $/; <DATA> }; print "\$var = \"$var\""; __DATA__ <a>a, b</a> <b>a,b</b> <b>a,b </b>
Then you can substitute against this variable as follows:
$var =~ s!<a>(.*?),(.*?)</a>!<a>$1A$2</a>!gs;
When you get that working, you might want to try with the following data:
__DATA__ This is to test. <a>a, b</a> <b>a,b</b> <b>a,b </b> <a>ab</a> <c>a,b</c> <a>a,b</a>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: trying to do a simple search
by texuser74 (Monk) on Jan 29, 2009 at 03:50 UTC |