C.R. am Dienstag, 31. Oktober 2006 17:20:
> In article <[EMAIL PROTECTED]>, [EMAIL PROTECTED] says...
>
> > You need to show us your code Chuck. Perl doesn't do that, in any
> > situation that I can think of. Try running this on its own:
> >
> > my $s = '144 cm';
> > $s =~ s/(\d+ +cm)/<bx;1>$1<ba>/g;
> > print $s;
> >
> > I get
> >
> > <bx;1>144 cm<ba>
> >
> > what do you get?
> >
> > That may help on its own. If not, like I said, post the relevant part of
> > your code.
>
> Your example above is extremely simple, and simply does not apply to my
> situation. But yes, that code above will work on my version of Perl,
> becasue Perl is only replacing one instance of /\d+ cm/. My situation is
> more complicated where I need to replace MULTIPLE instances of /\d+ cm/
> in a single string.
>
> My first post in this thread shows example data as it is stored in a
> scalar variable. It also shows what the string SHOULD look like after
> the substitution.
>
> Or maybe, perl simply is not able to replace multiple instances of a
> regex expression in a single scalar/string variable.
>
> $s="54 x 34 x 30-3/4 H<l>137 x 86 x 78 cm<l>Kneehole Height: 24-1/2``
> (62 cm)<l>Chair height: 30-3/4 (78 cm)<l>";
>
> (Don't worry about special strings like <l>, they are used by our
> typesetting software.) Notice that 78 cm appears twice, both should
> have <bx;1><ba> around them.
>
> $s should end up like this:
> 54 x 34 x 30-3/4 H<l>137 x 86 x <bx;1>78 cm<ba><l>Kneehole Height:
> 24-1/2`` (<bx;1>62 cm<ba>)<l>Chair height: 30-3/4 (<bx;1>78 cm<ba>)<l>
>
> Notice the insertion of <bx;1> and <ba> around strings that match
> /\d+ cm/.

Hello Chuck (again)

Have a look at the code of your first posting:

[Chuck:]
> while ($s=~m/\d+ +cm/g)
>      {
>      $old=$&; # Save current match.
>      $new=$old;
>      $s=~s/$old/<bx;1>$new<ba>/;
>      } # while

That's too complicated and thus also error prone. You don't need a loop to 
replace all occurances in a string. The /g modifier is here to do that.

Simply replace /all/ above lines with:

$s=~s/(\d+ +cm)/<bx;1>$1<ba>/g;
 
(or any of the variants presented by others)

I hope this helps.

Dani

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to