in reply to Re^2: Global substitution and report
in thread Global substitution and report

You could try to push the value of pos onto an array.

$str =~ s{$pattern}{push @positions, pos $str; $substitution}eg

I never worked with pos so I don't know if that helps you.

Replies are listed 'Best First'.
Re^4: Global substitution and report
by Outaspace (Scribe) on Aug 27, 2007 at 10:14 UTC
    It is nearly perfect, but
    $wFound = $szText =~ s/$szSearchText/push @wPos, pos $szText;$szReplac +eText/eg; foreach my $Line (@wLines) { $wLine = Convert::posToLine($szText, $Line); if ($wLines[-1] != $wLine) { push @wLines, $wLine; } print "Line: $wLine\n"; }
    doesn't replace correct if the replacement is something like "\L$1\E".
      There are two possible workarounds:

      A string eval, e.g.

      my $subst = 'lc $1'; # and later $str =~ s{$pattern}{push @pos, pos $str; eval $subst; }eg

      The other one moves the push to the regex:

      use strict; use warnings; use Data::Dumper; my @pos; my $str = "foobarbaz"; if ($str =~ s{([aeiou]+)(?{ push @pos, pos $str })}{<--$1-->}g){ print Dumper \@pos; print $str, "\n"; }

      But please read the notes in perlre on the (?{...}) construct.

        Yes the second workaround is exactly what I needed

        Thanks