Date: Sun, 23 Sep 2001 02:33:40 GMT From: Bob Walton Subject: Re: extra newline in s command Message-Id: <3BAD49B2.DE7AC769@rochester.rr.com> laura fairhead wrote: > > On Sat, 22 Sep 2001 13:50:28 -0700, "Jürgen Exner" wrote: > > >"laura fairhead" wrote in message > >news:3bace3a7.94715075@news.cis.dfn.de... ... > yes, I can't use 'chomp' because I'm working in Perl 4. ... > The replace in the s command is interpretted as a regular > expression and I need to just do a plain string replace. > The /$k[$1]/ must not be interpretted as a regular expression, > and it may have special characters in it. > > I've been reading as much as I can and looked at posts > in this group but I can't find anything. Doesn't > perl have a plain string replace function or does the > r.h.s have to be an perl RE ? > > I read something here about using \Q to 'quote meta' > but it doesn't seem to be working here. Is that another > of the things new in Perl 5 ? > > All I can see at the moment is messing about with > string splicing, using index() and then substr() to > do it but that lookz awful really. ... The "replacement string" in s/// is exactly what it says: a replacement string. The string is interpolated once, but it is *not* a regex. But...I don't know about Perl 4 -- I assume that is true for Perl 4, but I have no way of testing that, nor docs etc. But I don't think that would have changed between Perl 4 and Perl 5. Your problem, though, is that the first $1 in your s/// is being interpolated into a regex, then it is set to undef (by the regex engine, in preparation for the pattern match), then it is set to whatever happens in the regex (if there were no parens, nothing happens). Then the undef result (or whatever was matched by the first set of parens in the pattern if there were any) is being used as the subscript for @k in the replacement string, causing the interpolation of the replacement string to be 0000. Maybe the following would work for you: chomp(@k=(qq[x000],)); while(<>){ /(\d{4})/&& $1<=$#k && ($a=$1,1) && s/$1/$k[$a]/; print; } __DATA__ pattern1 pattern2 pattern3 It looks like that works to me. -- Bob Walton