Gossamer Forum
Home : General : Perl Programming :

simple regex replace problem

Quote Reply
simple regex replace problem
hello,

i'm creating a script in dos that parses some HTML and the following code snippet will not work properly:

my $cr = chr(13).chr(10);
my $text =~ s/$cr/<br>/g;
print $text;

i am printing to the DOS console in this case and the \n\r characters do not get removed when printed out... as a matter of fact, this snippet of code nukes the whole string and i end up with an empty string....

thanks if anyone can help. :-)
Quote Reply
Re: [mozkill] simple regex replace problem In reply to
You are using:

my $text =~ s/$cr/<br>/g;

...so you are trying to do a regex on a newly initialized scalar that has no value.

If you turn on warning (-w) you'll get a nice little message :)

Code:
perl -we "my $foo =~ s/a/b/g;"

Last edited by:

Paul: Jun 30, 2003, 2:44 PM
Quote Reply
Re: [Paul] simple regex replace problem In reply to
nope, the scalar is already equal to the entire string of the webpage response data:

i just tried this also, and i get the same problem:

my $text =~s/\x0d|\x0a//g;
print $text;

i know the data is initialized because if i remove the first line (above) the hmtl chunks print out fine except that that there are carriage return line feeds in the printed data which i want to remove...

my script starts out with these declarations:

use strict;
use diagnostics-verbose;
use LWP;
#LWP::Debug::level('+conns');
#LWP::Debug::level('+trace');
#LWP::Debug::level('+debug');
use HTML::TokeParser;
use HTTP::Cookies;
use HTTP::Request;
use HTTP::Response;
use Time::Local;

Last edited by:

mozkill: Jun 30, 2003, 3:02 PM
Quote Reply
Re: [mozkill] simple regex replace problem In reply to
thanks Paul! you got me thinking that i may have a typo and i found it. i was declaring "my $text" twice in the same scope... oops!!! it works now. :-)

sub printEventSummary
{
my $tmp_pg = $_[0];
my $p = HTML::TokeParser->new(\$tmp_pg);
while (my $token = $p->get_tag("td"))
{
my $cls = $token->[1]{class} || "-";
if ( $cls =~ /medtext/g ) # this matches the items with medtext class only
{
my $text = $p->get_text("/td");
$text =~s/\x0d|\x0a//g;
print $text;
}
}
}

Last edited by:

mozkill: Jun 30, 2003, 3:06 PM