Have perl increment a number that shows up before a delimiter

Discussion in 'Perl' started by john brown, Oct 21, 2003.

  1. john brown

    john brown Guest

    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
    john brown, Oct 21, 2003
    #1
    1. Advertising

  2. john brown

    Purl Gurl Guest

    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
    Purl Gurl, Oct 21, 2003
    #2
    1. Advertising

  3. john brown

    Jim Gibson Guest

    In article <>, john
    brown <> 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
    Jim Gibson, Oct 22, 2003
    #3
  4. john brown

    john brown Guest

    Purl Gurl <> wrote in message news:<>...
    > 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!
    john brown, Oct 22, 2003
    #4
  5. john brown

    john brown Guest

    Jim Gibson <> wrote in message news:<211020031716425322%>...
    > In article <>, john
    > brown <> 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?
    john brown, Oct 22, 2003
    #5
  6. john brown

    Roy Johnson Guest

    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;
    Roy Johnson, Oct 22, 2003
    #6
  7. john brown

    Jim Gibson Guest

    In article <>, john brown
    <> wrote:

    > Jim Gibson <> wrote in message
    > news:<211020031716425322%>...
    > > In article <>, john
    > > brown <> 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.
    Jim Gibson, Oct 22, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gce
    Replies:
    0
    Views:
    749
  2. Replies:
    104
    Views:
    10,926
    Jordan Abel
    Oct 28, 2005
  3. Replies:
    99
    Views:
    2,473
    eliza81
    Jun 11, 2010
  4. Alf P. Steinbach /Usenet
    Replies:
    0
    Views:
    878
    Alf P. Steinbach /Usenet
    May 22, 2011
  5. Peng Yu

    post increment or pre increment?

    Peng Yu, Nov 21, 2008, in forum: Perl Misc
    Replies:
    7
    Views:
    503
    Peter J. Holzer
    Nov 23, 2008
Loading...

Share This Page