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 ++;
}

3 Answers 3

15
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.

0

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
0

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.

6
  • 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
    Apr 11, 2009 at 3:53
  • I don't see anything in this answer that is a 5.10 feature. Apr 11, 2009 at 4:13
  • 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 ) Apr 11, 2009 at 4:29
  • oh, checked perldelta, I misread, @+ and @- only had a behaviour change. and \G isn't new either, statement redacted. Apr 11, 2009 at 4:32
  • And my javascript + php past poisoned me against eval :/, because in those languages, its almost always the wrong choice. Apr 11, 2009 at 4:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.