469,352 Members | 1,782 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

Have perl increment a number that shows up before a delimiter

I'm new to perl programming and would like some help if at all
possible. I'm trying to make a simple script where I can input a
filename. To be exact something like "/home/song1.mp3". I want to
introduce this on the command line and have the script increment this
file 9 times.I.e. /home/song2.mp3, /home/song3.mp3
/home/song4.mp3, etc. I suppose the period, ".", would be the
delimiter. I've put down the basics but I'm stumped with the body of
the program. Any help would be appreciated. I posted the little bit
I came up with.

#!/usr/bin/perl
use strict;
use warnings;
print "Input mp3 name ";
my $mp3;

# Maybe chomp the newline here

# Find the delimiter and increment the filename

# Loop it 9 times
Jul 19 '05 #1
6 6224
john brown wrote:

To be exact something like "/home/song1.mp3". I want to
introduce this on the command line and have the script increment this
file 9 times. /home/song2.mp3, /home/song3.mp3 /home/song4.mp3

Change my $path variable to:
"/home/song10.mp3"

"/home/song.mp3"
Note results carefully, then give those results thought.
Purl Gurl
--
Corvette Mako Sharks! 56 Chevy Napco 4X4!
http://www.purlgurl.net/~godzilla/
#!perl

$path = "/home/song1.mp3";

$count = substr ($path, rindex ($path, ".") - 1, 1);

for ($count .. 9)
{ substr ($path, rindex ($path, ".") - 1, 1, $count++); print "$path\n"; }
PRINTED RESULTS:
________________

/home/song1.mp3
/home/song2.mp3
/home/song3.mp3
/home/song4.mp3
/home/song5.mp3
/home/song6.mp3
/home/song7.mp3
/home/song8.mp3
/home/song9.mp3
Jul 19 '05 #2
In article <54**************************@posting.google.com >, john
brown <cg*******@hotmail.com> wrote:
I'm new to perl programming and would like some help if at all
possible. I'm trying to make a simple script where I can input a
filename. To be exact something like "/home/song1.mp3". I want to
introduce this on the command line and have the script increment this
file 9 times.I.e. /home/song2.mp3, /home/song3.mp3
/home/song4.mp3, etc. I suppose the period, ".", would be the
delimiter. I've put down the basics but I'm stumped with the body of
the program. Any help would be appreciated. I posted the little bit
I came up with.

#!/usr/bin/perl
use strict;
use warnings;
print "Input mp3 name ";
my $mp3;

# Maybe chomp the newline here
There is no newline if you have entered the filename on the command
line. What you entered is in $ARGV[0] (you should check $#ARGV to see
if you actually entered anything).

# Find the delimiter and increment the filename
You might have other periods in the filename, so it is best to look for
the substring '.mp3' in a regular expression.

# Loop it 9 times


You want to use a regular expression that matches a number before the
'.mp3', extracts the number and whatever was before it, and increments
the number in a loop. Something like this will work for any number of
digits in the number:

#!/opt/perl/bin/perl
use strict;
use warnings;
if( $ARGV[0] =~ /(\S+)(\d+)\.mp3$/ ) {
my $base = $1;
my $num = $2;
print $base . $num++ . ".mp3\n" for (1..9) ;
}

See perldoc perlre
Jul 19 '05 #3
Purl Gurl <pu******@purlgurl.net> wrote in message news:<3F***************@purlgurl.net>...
john brown wrote:

To be exact something like "/home/song1.mp3". I want to
introduce this on the command line and have the script increment this
file 9 times.

/home/song2.mp3, /home/song3.mp3 /home/song4.mp3

Change my $path variable to:
"/home/song10.mp3"

"/home/song.mp3"
Note results carefully, then give those results thought.
Purl Gurl
--
Corvette Mako Sharks! 56 Chevy Napco 4X4!
http://www.purlgurl.net/~godzilla/
#!perl

$path = "/home/song1.mp3";

$count = substr ($path, rindex ($path, ".") - 1, 1);

for ($count .. 9)
{ substr ($path, rindex ($path, ".") - 1, 1, $count++); print "$path\n"; }
PRINTED RESULTS:
________________

/home/song1.mp3
/home/song2.mp3
/home/song3.mp3
/home/song4.mp3
/home/song5.mp3
/home/song6.mp3
/home/song7.mp3
/home/song8.mp3
/home/song9.mp3

Thanks for the quick reply. Works!
Jul 19 '05 #4
Jim Gibson <jg*****@mail.arc.nasa.gov> wrote in message news:<211020031716425322%jg*****@mail.arc.nasa.gov >...
In article <54**************************@posting.google.com >, john
brown <cg*******@hotmail.com> wrote:
I'm new to perl programming and would like some help if at all
possible. I'm trying to make a simple script where I can input a
filename. To be exact something like "/home/song1.mp3". I want to
introduce this on the command line and have the script increment this
file 9 times.I.e. /home/song2.mp3, /home/song3.mp3
/home/song4.mp3, etc. I suppose the period, ".", would be the
delimiter. I've put down the basics but I'm stumped with the body of
the program. Any help would be appreciated. I posted the little bit
I came up with.

#!/usr/bin/perl
use strict;
use warnings;
print "Input mp3 name ";
my $mp3;

# Maybe chomp the newline here


There is no newline if you have entered the filename on the command
line. What you entered is in $ARGV[0] (you should check $#ARGV to see
if you actually entered anything).

# Find the delimiter and increment the filename


You might have other periods in the filename, so it is best to look for
the substring '.mp3' in a regular expression.

# Loop it 9 times


You want to use a regular expression that matches a number before the
'.mp3', extracts the number and whatever was before it, and increments
the number in a loop. Something like this will work for any number of
digits in the number:

#!/opt/perl/bin/perl
use strict;
use warnings;
if( $ARGV[0] =~ /(\S+)(\d+)\.mp3$/ ) {
my $base = $1;
my $num = $2;
print $base . $num++ . ".mp3\n" for (1..9) ;
}

See perldoc perlre


The initial code actually works pretty well. I did modify it to look
for ".mp3" instead of just ".". I've also tried to modify it to write
it's output to a file instead of STDOUT with no luck. I've been
placing "printf OUTPUTFILE "$path";" inside the loop but receive
errors. I would like to increment the original name and have it write
to a file. Right now the script of course writes the last result
only, i.e. /home/mysongs/mysong10.mp3". Any ideas?
Jul 19 '05 #5
This newsgroup is defunct. Use comp.lang.perl.misc instead. Or perl.beginners.

This increments the first string of digits that are followed by a dot.

my $song = '/home/song1.mp3';
for (0..9) {
print "$song\n";
$song =~ s/(\d+)\./(1+$1).'.'/e;
}

Here's a variation on Jim Gibson's, but with more shortcuts:

my $song = '/home/song1.mp3';
my ($num) = ($song =~ /(\d+)\./);
print $` . $num++ . ".$'\n" for 0..9;
Jul 19 '05 #6
In article <54*************************@posting.google.com> , john brown
<cg*******@hotmail.com> wrote:
Jim Gibson <jg*****@mail.arc.nasa.gov> wrote in message
news:<211020031716425322%jg*****@mail.arc.nasa.gov >...
In article <54**************************@posting.google.com >, john
brown <cg*******@hotmail.com> wrote:
I'm new to perl programming and would like some help if at all
possible. I'm trying to make a simple script where I can input a
filename. To be exact something like "/home/song1.mp3". I want to
introduce this on the command line and have the script increment this
file 9 times.I.e. /home/song2.mp3, /home/song3.mp3
/home/song4.mp3, etc. I suppose the period, ".", would be the
delimiter. I've put down the basics but I'm stumped with the body of
the program. Any help would be appreciated. I posted the little bit
I came up with.

#!/usr/bin/perl
use strict;
use warnings;
print "Input mp3 name ";
my $mp3;

# Maybe chomp the newline here


There is no newline if you have entered the filename on the command
line. What you entered is in $ARGV[0] (you should check $#ARGV to see
if you actually entered anything).

# Find the delimiter and increment the filename


You might have other periods in the filename, so it is best to look for
the substring '.mp3' in a regular expression.

# Loop it 9 times


You want to use a regular expression that matches a number before the
'.mp3', extracts the number and whatever was before it, and increments
the number in a loop. Something like this will work for any number of
digits in the number:

#!/opt/perl/bin/perl
use strict;
use warnings;
if( $ARGV[0] =~ /(\S+)(\d+)\.mp3$/ ) {
my $base = $1;
my $num = $2;
print $base . $num++ . ".mp3\n" for (1..9) ;
}

See perldoc perlre


The initial code actually works pretty well. I did modify it to look
for ".mp3" instead of just ".". I've also tried to modify it to write
it's output to a file instead of STDOUT with no luck. I've been
placing "printf OUTPUTFILE "$path";" inside the loop but receive
errors. I would like to increment the original name and have it write
to a file. Right now the script of course writes the last result
only, i.e. /home/mysongs/mysong10.mp3". Any ideas?


Are you still having problems? If so, it would be best to post the
exact code you are using, but only as much as required to demonstrate
the problem. Are you having trouble writing to a file? What errors are
you receiving?

By the way, this newsgroup is defunct. Try comp.lang.perl.misc in the
future.
Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Mark Wilson CPU | last post: by
1 post views Thread by Dunc | last post: by
reply views Thread by Mike Chirico | last post: by
1 post views Thread by Jim Bowe | last post: by
3 posts views Thread by Stang1 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.