Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Increment a string and write it to a file after each result.

Reply
Thread Tools

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

 
 
john brown
Guest
Posts: n/a
 
      10-23-2003
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;
 
Reply With Quote
 
 
 
 
J. Gleixner
Guest
Posts: n/a
 
      10-23-2003
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";
}

 
Reply With Quote
 
 
 
 
john brown
Guest
Posts: n/a
 
      10-24-2003
"J. Gleixner" <(E-Mail Removed)> wrote in message news:<QgTlb.1589$(E-Mail Removed)>...
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Post increment ++ has higher precedence than pre increment ++. Why? Alf P. Steinbach /Usenet C++ 0 05-22-2011 12:03 PM
why prefix increment is faster than postfix increment? jrefactors@hotmail.com C++ 99 06-11-2010 12:51 PM
Application that can auto-increment my clipboard value each time Ipaste Rats NZ Computing 8 10-10-2009 06:19 AM
post increment or pre increment? Peng Yu Perl Misc 7 11-23-2008 11:44 PM
why prefix increment is faster than postfix increment? jrefactors@hotmail.com C Programming 104 10-27-2005 11:44 PM



Advertisments