Have perl increment a number that shows up before a delimiter

J

john brown

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
 
P

Purl Gurl

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
 
J

Jim Gibson

john said:
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
 
J

john brown

Purl Gurl said:
john brown wrote:





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!
 
J

john brown

Jim Gibson said:
john said:
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?
 
R

Roy Johnson

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;
 
J

Jim Gibson

john brown said:
Jim Gibson said:
john said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top