Have perl increment a number that shows up before a delimiter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • john brown

    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
  • Purl Gurl

    #2
    Re: Have perl increment a number that shows up before a delimiter

    john brown wrote:

    [color=blue]
    > 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.[/color]
    [color=blue]
    > /home/song2.mp3, /home/song3.mp3 /home/song4.mp3[/color]


    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!



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

    Comment

    • Jim Gibson

      #3
      Re: Have perl increment a number that shows up before a delimiter

      In article <543320e0.03102 01944.563fbe4d@ posting.google. com>, john
      brown <cglobal25@hotm ail.com> wrote:
      [color=blue]
      > 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[/color]

      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).
      [color=blue]
      >
      > # Find the delimiter and increment the filename[/color]

      You might have other periods in the filename, so it is best to look for
      the substring '.mp3' in a regular expression.
      [color=blue]
      >
      > # Loop it 9 times[/color]

      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

      Comment

      • john brown

        #4
        Re: Have perl increment a number that shows up before a delimiter

        Purl Gurl <purlgurl@purlg url.net> wrote in message news:<3F94C52F. E177F03A@purlgu rl.net>...[color=blue]
        > john brown wrote:
        >
        >[color=green]
        > > 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.[/color]
        >[color=green]
        > > /home/song2.mp3, /home/song3.mp3 /home/song4.mp3[/color]
        >
        >
        > 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[/color]


        Thanks for the quick reply. Works!

        Comment

        • john brown

          #5
          Re: Have perl increment a number that shows up before a delimiter

          Jim Gibson <jgibson@mail.a rc.nasa.gov> wrote in message news:<211020031 716425322%jgibs on@mail.arc.nas a.gov>...[color=blue]
          > In article <543320e0.03102 01944.563fbe4d@ posting.google. com>, john
          > brown <cglobal25@hotm ail.com> wrote:
          >[color=green]
          > > 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[/color]
          >
          > 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).
          >[color=green]
          > >
          > > # Find the delimiter and increment the filename[/color]
          >
          > You might have other periods in the filename, so it is best to look for
          > the substring '.mp3' in a regular expression.
          >[color=green]
          > >
          > > # Loop it 9 times[/color]
          >
          > 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[/color]

          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?

          Comment

          • Roy Johnson

            #6
            Re: Have perl increment a number that shows up before a delimiter

            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;

            Comment

            • Jim Gibson

              #7
              Re: Have perl increment a number that shows up before a delimiter

              In article <543320e0.03102 20633.d8eb2fb@p osting.google.c om>, john brown
              <cglobal25@hotm ail.com> wrote:
              [color=blue]
              > Jim Gibson <jgibson@mail.a rc.nasa.gov> wrote in message
              > news:<211020031 716425322%jgibs on@mail.arc.nas a.gov>...[color=green]
              > > In article <543320e0.03102 01944.563fbe4d@ posting.google. com>, john
              > > brown <cglobal25@hotm ail.com> wrote:
              > >[color=darkred]
              > > > 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[/color]
              > >
              > > 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).
              > >[color=darkred]
              > > >
              > > > # Find the delimiter and increment the filename[/color]
              > >
              > > You might have other periods in the filename, so it is best to look for
              > > the substring '.mp3' in a regular expression.
              > >[color=darkred]
              > > >
              > > > # Loop it 9 times[/color]
              > >
              > > 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[/color]
              >
              > 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?[/color]

              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.

              Comment

              Working...