Increment a string and write it to a file after each result.

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

  1. john brown

    john brown Guest

    This script receives input from the command line. I put in for
    example "www.musicdownload.com/song1.mp3". The script looks for the
    ".mp3" extension and increments the preceeding number to 10. This
    works fine, printing the result to the screen. As you can see, I
    would like to write the results to a file called "wget_input". Wget
    reads from this file and the songs are retrieved. The problem I have
    is with writing all of the results to the file, in this
    case/www.musicdownload.com/song10.mp3" gets written to the file only.
    I know it must be incremented and written after each result, but I
    don't know how to go about constructing the loop. Any ideas?



    #! /usr/bin/perl -w
    #
    # mp3_search
    #

    print "Input path to video archive\n";
    $stdinput = <STDIN> ;
    chomp ($stdinput);
    $path = "$stdinput";

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

    for ($count .. 10)
    { substr ($path, rindex ($path, ".") - 1, 1, $count++); print
    "$path\n"; }

    open (OUTPUTFILE,">wget_input")|| die "Failed to open
    wget_input\n";

    printf OUTPUTFILE "$path";

    close OUTPUTFILE;
    john brown, Oct 23, 2003
    #1
    1. Advertising

  2. john brown

    J. Gleixner Guest

    john brown wrote:
    > This script receives input from the command line. I put in for
    > example "www.musicdownload.com/song1.mp3". The script looks for the
    > ".mp3" extension and increments the preceeding number to 10. This
    > works fine, printing the result to the screen. As you can see, I
    > would like to write the results to a file called "wget_input". Wget
    > reads from this file and the songs are retrieved. The problem I have
    > is with writing all of the results to the file, in this
    > case/www.musicdownload.com/song10.mp3" gets written to the file only.
    > I know it must be incremented and written after each result, but I
    > don't know how to go about constructing the loop. Any ideas?
    >
    >
    >
    > #! /usr/bin/perl -w
    > #
    > # mp3_search
    > #
    >
    > print "Input path to video archive\n";
    > $stdinput = <STDIN> ;
    > chomp ($stdinput);
    > $path = "$stdinput";
    >
    > $count = substr ($path, rindex ($path, ".mp3") - 1, 1);
    >
    > for ($count .. 10)
    > { substr ($path, rindex ($path, ".") - 1, 1, $count++); print
    > "$path\n"; }
    >
    > open (OUTPUTFILE,">wget_input")|| die "Failed to open
    > wget_input\n";
    >
    > printf OUTPUTFILE "$path";
    >
    > close OUTPUTFILE;


    In place of printing it to the screen, print it to your OUTPUTFILE in
    your loop. Using your code, that'd be something like:

    open (OUTPUTFILE,">wget_input")|| die "Failed to open wget_input: $!\n";
    #include reason
    for ($count .. 10)
    {
    substr ($path, rindex ($path, ".") - 1, 1, $count++);
    print OUTPUTFILE "$path\n";
    }
    close (OUTPUTFILE);

    I'd probably get the path, up to the integer, and then just print it and
    forget about the multiple calls to substr, or do the index once, and
    store it before the for loop.

    for ($count .. 10)
    {
    print OUTPUTFILE $new_path, $count++, ".mp3\n";
    }
    J. Gleixner, Oct 23, 2003
    #2
    1. Advertising

  3. john brown

    john brown Guest

    "J. Gleixner" <> wrote in message news:<QgTlb.1589$>...
    > john brown wrote:
    > > This script receives input from the command line. I put in for
    > > example "www.musicdownload.com/song1.mp3". The script looks for the
    > > ".mp3" extension and increments the preceeding number to 10. This
    > > works fine, printing the result to the screen. As you can see, I
    > > would like to write the results to a file called "wget_input". Wget
    > > reads from this file and the songs are retrieved. The problem I have
    > > is with writing all of the results to the file, in this
    > > case/www.musicdownload.com/song10.mp3" gets written to the file only.
    > > I know it must be incremented and written after each result, but I
    > > don't know how to go about constructing the loop. Any ideas?
    > >
    > >
    > >
    > > #! /usr/bin/perl -w
    > > #
    > > # mp3_search
    > > #
    > >
    > > print "Input path to video archive\n";
    > > $stdinput = <STDIN> ;
    > > chomp ($stdinput);
    > > $path = "$stdinput";
    > >
    > > $count = substr ($path, rindex ($path, ".mp3") - 1, 1);
    > >
    > > for ($count .. 10)
    > > { substr ($path, rindex ($path, ".") - 1, 1, $count++); print
    > > "$path\n"; }
    > >
    > > open (OUTPUTFILE,">wget_input")|| die "Failed to open
    > > wget_input\n";
    > >
    > > printf OUTPUTFILE "$path";
    > >
    > > close OUTPUTFILE;

    >
    > In place of printing it to the screen, print it to your OUTPUTFILE in
    > your loop. Using your code, that'd be something like:
    >
    > open (OUTPUTFILE,">wget_input")|| die "Failed to open wget_input: $!\n";
    > #include reason
    > for ($count .. 10)
    > {
    > substr ($path, rindex ($path, ".") - 1, 1, $count++);
    > print OUTPUTFILE "$path\n";
    > }
    > close (OUTPUTFILE);
    >
    > I'd probably get the path, up to the integer, and then just print it and
    > forget about the multiple calls to substr, or do the index once, and
    > store it before the for loop.
    >
    > for ($count .. 10)
    > {
    > print OUTPUTFILE $new_path, $count++, ".mp3\n";
    > }



    Thanks for the help. Works like a charm.
    john brown, Oct 24, 2003
    #3
    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. Replies:
    104
    Views:
    10,995
    Jordan Abel
    Oct 28, 2005
  2. Replies:
    99
    Views:
    2,491
    eliza81
    Jun 11, 2010
  3. Alf P. Steinbach /Usenet
    Replies:
    0
    Views:
    889
    Alf P. Steinbach /Usenet
    May 22, 2011
  4. Michael Tan
    Replies:
    32
    Views:
    965
    Ara.T.Howard
    Jul 21, 2005
  5. Peng Yu

    post increment or pre increment?

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

Share This Page