473,398 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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 6381
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mark Wilson CPU | last post by:
This must be easy, but I'm missing something... I want to execute a Perl script, and capture ALL its output into a PHP variable. Here are my 2 files: -------------------------------------...
1
by: Dunc | last post by:
I'm new to Postgres, and getting nowhere with a PL/Perl trigger that I'm trying to write - hopefully, someone can give me some insight into what I'm doing wrong. My trigger is designed to reformat...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
3
by: Jeanne | last post by:
I am working on a cgi script that is suppose to pop-up a javascript box from the following perl variables:$TodayDate, $LinkCity, $LinkState. I recently encountered a problem with the $LinkCity...
1
by: Jim Bowe | last post by:
I converted to a DB2 database where the data type was changed from autonumber to number field. I still need to increment the field by one each time a new record is added. Can anyone help me? ...
5
by: Stuart | last post by:
Hi all, Iv'e got a page that has a mass amount of input fields, all of which require a decimal figure. To make it easier when it comes to inputting data, I'm trying to setup + and - links that...
4
by: mebbert | last post by:
I was testing the increment operator in Perl and found a different behavior from other languanges. my $i = 1; print ++$i + ++$i . "\n"; The above code prints out the answer 6, instead of 5 as...
3
by: Stang1 | last post by:
The following statement: line_buf = ' '; is equivalent to: line_buf = ' '; line_len++;
11
by: doraima29 | last post by:
Hi, I am a newbie at PERL and really wanted to understand how server-side programming really works and operates since I use at the workplace. I use ASP and wanted to learn more about server-side...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.