From: "Dr.Ruud" <[EMAIL PROTECTED]>
> "Jenda Krynicky" schreef:
>
> > Having a string like this:
> >
> > $version = 'Version: 1.47.785';
> >
> > increment the last number. I seriously doubt you can do anything even
> > remotely as simple as
> >
> > $version =~ s/^(Version:\s*(?:\d+\.)*)(\d+)/$1 . ($2+1)/e;
>
> - never trust your own (often temporary) definition of simple.
> - be careful when to use \d, that set can contain more than 100
> characters.
Beg your pardon?
> - you are using string evaluation. (read perlretut again)
No I'm not. It's a single /e, not double /ee. The stuff inside the
matched string is not evaluated as Perl code. Read perlretut again.
> - you don't offer an easy choice of which part to increment.
That was not in the original request. Don't
> Alternative way, without string evaluation, using split with a simple
> regex:
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> my $version = 'Version: 1.47.785';
>
> my @parts = split /[.]/, $version;
No need for a character group:
my @parts = split /\./, $version;
> ++$parts[-1];
>
> $version = join ".", @parts;
>
> print $version, "\n";
> __END__
>
>
> > Make sure it works for
> >
> > $version = 'Version:1.47.789';
> >
> > as well. Without string eval() if I may ask.
>
> Make sure it works for 1.02.003 as well, without string eval, and with
> easy choice of incrementing what part.
Rinse and repeat. There is no string eval in my code, incrementing
other parts was not wanted. You are right about the .003 though, I
stand corrected on that one.
$version =~ s/^(Version:\s*(?:\d+\.)*)(\d+)/my $ver = $2; ++$ver,
$1 . $ver/e;
OTOH, your original code using the version strings did not even
support anything like 1.02.003.
> So version_inc("Version:1.02.003", -2, 1) should return
> "Version:1.03.000".
> And version_inc(" Version : 1.02.003 ", 1, 1) should likely return "
> Version : 2.00.000 ".
> Without cvs if I may ask.
If this was required then I'd use split() as well
sub version_inc {
my ($str, $part, $by) = @_;
my ($prefix, $ver) = ($str =~ /^(.*?)((?:\d+\.)*\d+)/)
or die 'Invalid format';
my @ver = split /\./, $ver;
$part-- if $part > 0; # OK, let's have it 1 based
$ver[$part] = sprintf( "%0" . length($ver[$part]) . "d",
$ver[$part] + $by);
if ($part != -1 and $part != $#ver) {
$part = @ver + $part if $part < 0;
tr/0-9/0/ for (@ver[$part+1 .. $#ver])
}
return $prefix . join( '.', @ver);
}
Jenda
===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/