tags:

views:

411

answers:

3

I want to replace a token in a text file with a number. The token is "<count>" and I want it to be replaced with the number of counts so far. For example:

This <count> is a <count> count.
The <count> count increases <count><count><count>.
<count><count><count><count><count><count>

becomes:

This 1 is a 2 count.
The 3 count increases 456.
789101112

I'm not really sure how to do this, maybe with some loop?

my $text = (the input from file, already taken care of in my script);
my $count = 1;
while( regex not found? )
{
    $text =~ s/<count>/($count);
    $count ++;
}
+13  A: 
my $text = "whatever";
my $count = 1;
$text =~ s/<count>/$count++/ge;

should do it for you. The /e at the end of the substitution makes all the difference.

Jim Puls
A: 

Here's a procFile script which does what you requested:

$val = 1;                         # Initial change value.
while (<STDIN>) {                 # Process all lines.
    chomp;                        # Remove linefeed.
    $ln = $_;                     # Save it.
    $oldln = "x" . $ln;           # Force entry into loop.
    while ($oldln ne $ln) {       # Loop until no more changes.
        $oldln = $ln;             # Set lines the same.
        $ln =~ s/<count>/$val/;   # Change one occurrence if we can.
        if ($oldln ne $ln) {      # Increment count if change was made.
            $val++;
        }
    }
    print "$ln\n";                # Print changed line.
}

You run it with cat inputFile | perl procFile and your sample file:

This <count> is a <count> count.
The <count> count increases <count><count><count>.
<count><count><count><count><count><count>

generates:

This 1 is a 2 count.
The 3 count increases 456.
789101112
paxdiablo
A: 

Here's another way to do it with a positional sweep.

It uses the \G positional reference which matches either the start of the line, or the position after the previous match.

#!/usr/bin/perl 
use strict;      # always strict + warnings 
use warnings;

my $count = 1;   # start at 1; 

while ( my $line = <STDIN> ) {                            # read stdin 
    while ( $line =~ /\G.*?(<count>)/g ) {                 # scan left to right and pick out one <count> at a time.
        substr( $line, $-[1], $+[1] - $-[1], $count++ );  # replace the substring and increment
    }
    print $line;
}

Its functionally almost identical to the posted regex-eval solution, just it works without eval. I earlier posted some anti-eval fear, but its just FUD from other lesser languages not doing eval safely.

The /e way is effectively doing this:

replace_callback( \$input, $regex, sub{ 
   return $count++;
});

( Where replace callback is some bloated function that does all the work )

Which really is safe as eggs, its just not obvious that it is that safe.

Kent Fredric
Yes, /e is an eval, but it's an eval BLOCK, not an eval EXPR; eval BLOCK is is very safe (it's just the way to trap errors) and people should not be paranoid of using it.
nohat
nope, /e is like eval EXPR.
brian d foy
I don't see anything in this answer that is a 5.10 feature.
brian d foy
I thought $- and $+ were new in 5.10, maybe I'm wrong :). And I seem to have gotten the idea that regexp evals were stringy based somehow ( possibly based on how syntax-highlighters tend to show it )
Kent Fredric
oh, checked perldelta, I misread, @+ and @- only had a behaviour change. and \G isn't new either, statement redacted.
Kent Fredric
And my javascript + php past poisoned me against eval :/, because in those languages, its almost *always* the wrong choice.
Kent Fredric
I seem to have gotten the idea that '$x =~ s/hello/bar $i++/e' would be valid regex, but it isn't, you need to do "$x =~ s/hello/'bar'. $i++/e" to get that result .
Kent Fredric