Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Increment Numbers

Reply
Thread Tools

Increment Numbers

 
 
Chris Ellison
Guest
Posts: n/a
 
      01-02-2006
Hi,


"Larry's filename fixer"

------filename: rename-------------
#!/usr/bin/perl -w
# rename - Larry's filename fixer
$op = shift or die "Usage: rename expr [files]\n";
chomp(@ARGV = <STDIN>) unless @ARGV;
for (@ARGV) {
$was = $_;
eval $op;
die $@ if $@;
rename($was,$_) unless $was eq $_;
}
----------------------------------

I have a bunch of files 001.txt, 002.txt, 055.txt, 099.txt,... and
would like to increment them using the above script.

So, I was thinking something like this:

rename 's/(\d{3})/something/' *

Though, I don't know how to accomplish the "something".
I was thinking that it could be done with backreferencing:

$1+increment

but I don't know how to do operations within a regular expression.

Any suggestions?
 
Reply With Quote
 
 
 
 
Xicheng Jia
Guest
Posts: n/a
 
      01-02-2006
you may use sprintf() and regex's 'e' modifier:

rename 's/^(\d{3})/sprintf("%03d",$1+1)/e' *.txt

Xicheng

Chris Ellison wrote:
> Hi,
>
>
> "Larry's filename fixer"
>
> ------filename: rename-------------
> #!/usr/bin/perl -w
> # rename - Larry's filename fixer
> $op = shift or die "Usage: rename expr [files]\n";
> chomp(@ARGV = <STDIN>) unless @ARGV;
> for (@ARGV) {
> $was = $_;
> eval $op;
> die $@ if $@;
> rename($was,$_) unless $was eq $_;
> }
> ----------------------------------
>
> I have a bunch of files 001.txt, 002.txt, 055.txt, 099.txt,... and
> would like to increment them using the above script.
>
> So, I was thinking something like this:
>
> rename 's/(\d{3})/something/' *
>
> Though, I don't know how to accomplish the "something".
> I was thinking that it could be done with backreferencing:
>
> $1+increment
>
> but I don't know how to do operations within a regular expression.
>
> Any suggestions?


 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      01-02-2006
Xicheng Jia <(E-Mail Removed)> wrote:

> you may use sprintf() and regex's 'e' modifier:



There is no such thing as an 'e' modifier for a regex.


> rename 's/^(\d{3})/sprintf("%03d",$1+1)/e' *.txt



Oh. You meant the _substitution operator's_ 'e' modifier.



[snip TOFU]


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      01-02-2006
Chris Ellison <(E-Mail Removed)> wrote:

> rename 's/(\d{3})/something/' *



> but I don't know how to do operations within a regular expression.



That's OK because you don't want to do an operation within
a regular expression, you want to do an operation within
the REPLACEMENT part of the s/// operator (which is (by default)
a string, not a regex).


> Any suggestions?



Read the entry for s/// in perlop.pod, until you get to this part:

e Evaluate the right side as an expression.


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Dave Weaver
Guest
Posts: n/a
 
      01-06-2006
<TOFU fixed>

On 1 Jan 2006 19:23:36 -0800, Xicheng Jia <(E-Mail Removed)> wrote:
> Chris Ellison wrote:
> > I have a bunch of files 001.txt, 002.txt, 055.txt, 099.txt,... and
> > would like to increment them using the above script.

>
> you may use sprintf() and regex's 'e' modifier:
>
> rename 's/^(\d{3})/sprintf("%03d",$1+1)/e' *.txt
>


But note that this will probably fail, depending on the order the
files are processed in.

If you start with 001.txt, and 002.txt, and it first renames 001.txt
to 002.txt, and then renames 002.txt to 003.txt, you'll find you've
lost a file and 003.txt is what was originally 001.txt.

You may be better off writing a small custom script to do the renaming
correctly.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Post increment ++ has higher precedence than pre increment ++. Why? Alf P. Steinbach /Usenet C++ 0 05-22-2011 12:03 PM
why prefix increment is faster than postfix increment? jrefactors@hotmail.com C++ 99 06-11-2010 12:51 PM
post increment or pre increment? Peng Yu Perl Misc 7 11-23-2008 11:44 PM
why prefix increment is faster than postfix increment? jrefactors@hotmail.com C Programming 104 10-27-2005 11:44 PM
Frame Numbers vs. JPEG Numbers With CF Cards mort Digital Photography 3 02-16-2005 01:43 AM



Advertisments