Bradford Ritchie wrote:
> Is it possible to take a string ($pat) do a substitution on
> it and print the result, without actually changing the
> contents of the original variable?
No; you have to do the subsitution on a copy.
>
> Basically, I'm trying to write a script that will take a
> string and look for a pattern in that line (which is
> guaranteed to be a number), increment it and print the new
> line, then increment it again an print it, etc., up to a
> specified number of times.
>
> Here's the start of some code I have:
> (I realize there's no error checking but right now I'm just
> focusing on the basic logic).
>
> use strict;
> my $pat = $ARGV[0];
> my $num = $ARGV[1];
>
> foreach( <STDIN> ) {
> print; ## print the original line
> foreach my $i ($pat .. ($pat+$num)) {
> s/$i/$i+1/e;
> print;
> }
> }
>
> A sample input line would look like this:
> <link id="31.101.18.0">
>
> And if called like this "%script.pl 18 5" I should expect to
> se <link id="31.101.18.0"> <link id="31.101.19.0"> <link
> id="31.101.20.0"> <link id="31.101.21.0"> <link
> id="31.101.22.0"> <link id="31.101.23.0"> <link id="31.101.24.0">
The code you posted produces this result when I run it.
>
> I'm running into several problems with this. It doesn't work
> because each substitution changes the original line. If I
> had specified 15 instead of 5 as the second parameter, the
> s/// would start matching the first 31 in the input line when
> $i eventually became 31.
>
> Ultimately, I want to be able to specify a bit of sample text
> like '.18.' which is more unique than simply '18'. Then have
> the script search for the more unique pattern but still be
> able to increment the number within it. (ie. '.19.' '.20.'
> '.21' ...) Note, that these would be literal dots, not regex
> wildcards.
Not sure exactly what you want to do. Do you just want to increment the
third number, whatever it is? Maybe you want something more like:
for my $i (1..$num) {
s/(\d+\.\d+\.)(\d+)/$1 . ($2+1)/e;
print;
}
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]