Re: Having problems with with the s/// substitution

From: Hakan Bacakoglu (hbacakoglu_at_hotmail.com)
Date: 03/17/04


Date: 17 Mar 2004 10:01:58 -0800

Thank you all for the response.

First, someone was asking for the sample data file:

-----------------------------------------------------
<TD BORDERCOLOR=#c0c0c0 ><FONT SIZE=2 FACE="Arial"
COLOR=#000000>0970</FONT></TD>
<TD BORDERCOLOR=#c0c0c0 ><FONT SIZE=2 FACE="Arial"
COLOR=#000000>0976</FONT></TD>
<TD BORDERCOLOR=#c0c0c0 ><FONT SIZE=2 FACE="Arial"
COLOR=#000000>0061</FONT></TD>
<TD BORDERCOLOR=#c0c0c0 ><FONT SIZE=2 FACE="Arial"
COLOR=#000000>0349<BR>0350<BR>0351<BR>0352<BR>0353<BR>0354<BR>0355<BR>0356<BR>0357<BR>0358</FONT></TD>
<TD BORDERCOLOR=#c0c0c0 ><FONT SIZE=2 FACE="Arial"
COLOR=#000000>125</FONT></TD>
<TD BORDERCOLOR=#c0c0c0 ><FONT SIZE=2 FACE="Arial"
COLOR=#000000><BR></FONT></TD>
-----------------------------------------------------

Second, I ended up using the following code (probably not a very
graceful code, but worked for a beginner like me). I will try the
other suggestions.

while (<WEBINP>){
    $codes = $_;
    $codes =~ s/(\D)(\d{4})(\D)/(&find($2,@list))/ge;
    print WEBOUT $codes;
}

hakan

Uri Guttman <uri@stemsystems.com> wrote in message news:<x7brmwp8t2.fsf@mail.sysarch.com>...
> >>>>> "BM" == Brian McCauley <nobull@mail.com> writes:
>
> BM> s/(?<=\D)(\d{4})(?=\D)/find("$1",@list)/eg;
> BM> s/(?<!\d)(\d{4})(?!\d)/find("$1",@list)/eg;
>
> BM> (Note: passing $digit as an argument is a situation where the normal
> BM> useless use of quotes rule does _not_ apply).
>
> on the contrary!
>
> the code in the replacement with a single /e is compile time just like
> eval BLOCK. it is not a string and it has normal perl syntax. look at
> this script:
>
> #!/usr/local/bin/perl
> while( <DATA> ) {
> s/(.*)/foo( $1 )/e ;
> print ;
> }
>
> sub foo {
> return uc shift ;
> }
> __DATA__
> foo bar
> more spaces
>
> perl ~/perl/test/sexec.pl
> FOO BAR
> MORE SPACES
>
> as you can see, the spaces were passed in fine in $1 and no quotes were
> needed.
>
> maybe you were thinking about /ee where the replacement must be a
> expression (usually a string) to be evaled and which the result will
> then also be eval'ed. the best thing there is to use qq or q to wrap the
> inner stuff if it has to have quotes.
>
> uri



Relevant Pages

  • Re: Having problems with with the s/// substitution
    ... BM> useless use of quotes rule does _not_ apply). ... the code in the replacement with a single /e is compile time just like ... it is not a string and it has normal perl syntax. ... FOO BAR ...
    (comp.lang.perl.misc)
  • Re: ARGV interpretation in s///
    ... > double quotes, or even a /ee without the extra double quotes. ... The replacement string part of s/// is double quotish, ... it looks like this to perl: ...
    (comp.lang.perl.misc)
  • Re: s/// parametrized with backreferences
    ... If double eval works with double quotes then this also should work ... Any expression that results in the string you want to use as ... the replacement text will do the job. ... In my original code, you could use any of the below to get the ...
    (comp.lang.perl.misc)
  • Re: How would you do this in Forth?
    ... You're given a string (just a count and chars in memory, ... Simple whitespace-separated tokens, with varying amounts of ...
    (comp.lang.forth)
  • Re: How would you do this in Forth?
    ... You're given a string (just a count and chars in memory, ... to chars plus a count, or a null-terminated string, doesn't matter) ... splitting it on whitespace boundaries but respecting escaping and ...
    (comp.lang.forth)