Forum Moderators: coopster & phranque

Message Too Old, No Replies

Perl regexp substitution problem

works an an m{} but not as an s{}{}

         

dingman

4:41 pm on Sep 24, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm beating my head against what I think is a simple Perl issue. I'm trying to munge data into a reasonably presentable form, and the last thing left is to translate "<tr><th>Location:</th><td> wsl</td></tr>" to "<tr><th>Location:</th><td> Workshop Research Library</td></tr>". I'm trying to do this with the line:

$bib =~ s{(Location:</th><td>)\s?(wsl)}{$1 Workshop Research Library}isg;

I added the line
if ($bib =~ m{(Location:</th><td>)\s?(wsl)}is) {print STDERR "$1$2\n";}
to my script to confirm that the expression truly is matching what it's supposed to, and the output I get with that line in place looks like the expression works. Can anyone help me figure out why the substitution doesn't work?

-dingman

volatilegx

4:50 pm on Sep 24, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just use:

$bib =~ s/<td> wsl<\/td>/<td> Workshop Research Library<\/td>/ig;

andreasfriedrich

4:53 pm on Sep 24, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I just tested your substitution with
perl -e ' 
$_="<tr><th>Location:</th><td> wsl</td></tr>";
s{(Location:</th><td>)\s?(wsl)}{$1 Workshop Research Library}isg;
print $_;'

It worked. The output was:

<tr><th>Location:</th><td> Workshop Research Library</td></tr>

dingman

4:56 pm on Sep 24, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the snippet, but it's not working, either. Any idea what was wrong with mine? I'd like to actually learn something from this rather than merely steal a line of code.

-dingman

volatilegx

5:02 pm on Sep 24, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Weird... my snippet works on my server.

Are you able to do other substitutions?

Does either snippet cause server errors or does the substitution just not work?

andreasfriedrich

5:05 pm on Sep 24, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Which one is not working either?

Have you tried it from the prompt?

Would you mind posting some more code surrounding the problematic line?

Are you sure that $bib contains the same string when you try the substitution and when you do the matching?

Since my code was working for me on my box IŽm assuming the error must be somewhere else.

What perl version are you running?

dingman

5:06 pm on Sep 24, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



andreas, what version of Perl are you using? The two machines I've just tried this on are using perl 5.6.0 and 5.6.1, and neither of them performs that substitution. It's one of dozens in the script, and also the last one before a 'return', after which nothing happens but printing the output. All the others work as designed.

I should also have been a bit more clear that some of the occurances have a <br> after wsl in the original $bib - hence the fact that I was not matching on anything after wsl. I don't have much control over the input, so I have to just deal with that.

-dingman

andreasfriedrich

5:07 pm on Sep 24, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Summary of my perl5 (revision 5.0 version 6 subversion 0) configuration: 
Platform:
osname=linux, osvers=2.2.17-8smp, archname=i386-linux
uname='linux porky.devel.redhat.com 2.2.17-8smp
#1 smp fri nov 17 16:12:17 est 2000 i686 unknown '
config_args='-des -Doptimize=-O2 -march=i386 -mcpu=i686
-Dcc=gcc -Dcccdlflags=-fPIC -Dinstallprefix=/usr
-Dprefix=/usr -Darchname=i386-linux -Dd_dosuid
-Dd_semctl_semun -Di_db -Di_ndbm -Di_gdbm -Di_shadow
-Di_syslog -Dman3ext=3pm -Uuselargefiles'
hint=recommended, useposix=true, d_sigaction=define
usethreads=undef use5005threads=undef useithreads=undef
usemultiplicity=undef
useperlio=undef d_sfio=undef uselargefiles=undef
use64bitint=undef use64bitall=undef uselongdouble=undef usesocks=undef
Compiler:
cc='gcc', optimize='-O2 -march=i386 -mcpu=i686', gccversion=2.96 20000731
(Red Hat Linux 7.1 2.96-78)
cppflags='-fno-strict-aliasing'
ccflags ='-fno-strict-aliasing'
stdchar='char', d_stdstdio=define, usevfork=false
intsize=4, longsize=4, ptrsize=4, doublesize=8
d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t',
lseeksize=4
alignbytes=4, usemymalloc=n, prototype=define
Linker and Libraries:
ld='gcc', ldflags =' -L/usr/local/lib'
libpth=/usr/local/lib /lib /usr/lib
libs=-lnsl -ldl -lm -lc -lcrypt
libc=/lib/libc-2.2.so, so=so, useshrplib=false, libperl=libperl.a
Dynamic Linking:
dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-rdynamic'
cccdlflags='-fPIC', lddlflags='-shared -L/usr/local/lib'

Characteristics of this binary (from libperl):
Compile-time options:
Built under linux
Compiled at Apr 3 2001 11:27:33
@INC:
/usr/lib/perl5/5.6.0/i386-linux
/usr/lib/perl5/5.6.0
/usr/lib/perl5/site_perl/5.6.0/i386-linux
/usr/lib/perl5/site_perl/5.6.0
/usr/lib/perl5/site_perl
.

dingman

5:13 pm on Sep 24, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



#translate a location of "wsl" to "Workshop Research Library"
#if ($bib =~ m{(Location:</th><td>)\s?(wsl)}is) {print STDERR "$1$2\n";}
$bib =~ s{(Location:</th><td>)\s?(wsl)}{$1 Workshop Research Library}isg;

$entry = "<html>\n\t<head>\n\t\t<title> $author\t$date\t$title</title>";
$entry .= "<link rel=\"stylesheet\" type=\"text/css\" ";
$entry .= "href=\"http://www.indiana.edu/~iascp/CSS/cprbib.css\">\n\t</head>\n";
$entry .= "\t<body>\n<table>\n$bib\n</table>\n</body>\n<!-- EndLogicalDocument -->\n";

return $entry;
}

I'll hapily post the whole 9k script somewhere you can download it if you're interested.

-dingman

dingman

5:20 pm on Sep 24, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



BTW, I don't doubt that it's my fault, and I really do appreciate your help. Having tested at the command prompt with just that substitution, it does seem to work as advertised *outside* of my script.

andreasfriedrich

5:22 pm on Sep 24, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you running this as a stand alone perl script or under mod_perl?

I just tested the substitution with perl 5.6.1 for Windows and it was working as well.

If youŽd send me the script using the address in my profile I'll gladly try to run your script either as a stand alone script or mod_perl module.

Andreas

dingman

5:36 pm on Sep 24, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's a stand-alone perl script that generates a file of hundreds of HTML documents concatenated together, and then a search engine sorts out which one you the user wants.

I think I might have just found my bone-head error. If not, I introduced a new one...

-dingman

dingman

5:42 pm on Sep 24, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm an embarassed idiot. There were <p>s around wsl and I didn't even see them in the output I was staring at.

Thanks for humoring me. I finally saw the <p> tags while I was composing an e-mail to Andreas in an adjacent window.

-dingman