From: David Gilden <[EMAIL PROTECTED]>
> How do get $i do increment inside the substitution below?
> Thanks
> Dave
>
> #!/usr/bin/perl -w
>
> my $i = 0;
>
> while(<>)
> {
> chomp;
> s/name=\"order/name=\"order$i++/;
> print "$_\n";
>
> }
You have to tell Perl to treat the replacement as Perl code:
s/name="order/$i++; 'name="order'/e;
Are you aware of the fact that this will only notice the
'name="order' once on each line? you probably want /g there:
s/name="order/$i++; 'name="order'/eg;
Notice though that you are not doing any changes to the string.
Therefore you should not be using s///.
If it's OK to only look for one 'name="order' on each line you may
use
$i++ if /name="order/;
if not you want this:
$i += (() = (/name="order/g));
OK. I admit it's a little strange.
Let's dissect it:
/name="order/g
in list context returns a list of all matches.
() = (/name="order/g)
provides the list context to the matching, but immediately forgets
the actuall matches.
$i +=
provides scalar context to the
() = (/name="order/g)
and since list assignment returns the number of elements of the
righthand list the result of
() = (/name="order/g)
in the scalar context is the number of matches.
Which we add to the $i.
Humpf.
Jenda
=============================================================
All those who understood the topic I just elucidated, please
verticaly extend your upper limbs.
-- Ted Goff
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]