For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > November 2004 > Substitution inside a loop









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Substitution inside a loop
Brian Volk

2004-11-25, 3:55 am

Hi All,

I have a .txt file which contains item numbers like so..
1234
1245
1278
1240
etc.
I am trying to print the entire url, $main_url after substituting the item
number in the .txt file... I'm having a little trouble w/ my loop... Below
is the script.

#!/usr/bin/perl -w

use strict;

my $kc_item = "E:/perl/kc_num.txt";
open (KCITEM, $kc_item) or die "can't open $kc_item: $!";

# my $new_file = "> E:/perl/kc_urls.txt";

my $main_url = "
<http://www.kcprofessional.com/us/pr...v1&searchtext=1
804&x=0&y=0>
http://www.kcprofessional.com/us/pr...1&searchtext=18
04&x=0&y=0";

while (<KCITEM> ) {
print "$main_url\n";
s/1804/KCITEM/ and print "$main_url\n";
}

close KCITEM;

Here is what the output should look like...

http://www.kcprofessional.com/us/pr...s.asp?search=v1
<http://www.kcprofessional.com/us/pr...v1&searchtext=1
234&x=0&y=0> &searchtext=1234&x=0&y=0
http://www.kcprofessional.com/us/pr...s.asp?search=v1
<http://www.kcprofessional.com/us/pr...v1&searchtext=1
245&x=0&y=0> &searchtext=1245&x=0&y=0
http://www.kcprofessional.com/us/pr...s.asp?search=v1
<http://www.kcprofessional.com/us/pr...v1&searchtext=1
278&x=0&y=0> &searchtext=1278&x=0&y=0
http://www.kcprofessional.com/us/pr...s.asp?search=v1
<http://www.kcprofessional.com/us/pr...v1&searchtext=1
240&x=0&y=0> &searchtext=1240&x=0&y=0
etc.

Any help would be greatly apprecitated!

Thanks!

Brian Volk
HP Products
317.298.9950 x1245
<mailto:bvolk@hpproducts.com> bvolk@hpproducts.com



Gunnar Hjalmarsson

2004-11-25, 3:55 am

Brian Volk wrote:
> I have a .txt file which contains item numbers like so..
> 1234
> 1245
> 1278
> 1240
> etc.
> I am trying to print the entire url, $main_url after substituting the item
> number in the .txt file... I'm having a little trouble w/ my loop... Below
> is the script.
>
> #!/usr/bin/perl -w
>
> use strict;
>
> my $kc_item = "E:/perl/kc_num.txt";
> open (KCITEM, $kc_item) or die "can't open $kc_item: $!";
>
> # my $new_file = "> E:/perl/kc_urls.txt";
>
> my $main_url = "
> <http://www.kcprofessional.com/us/pr...v1&searchtext=1
> 804&x=0&y=0>
> http://www.kcprofessional.com/us/pr...1&searchtext=18
> 04&x=0&y=0";
>
> while (<KCITEM> ) {


You want to remove the newline character:

chomp;

> print "$main_url\n";
> s/1804/KCITEM/ and print "$main_url\n";


You probably mean:

$main_url =~ s/1804/$_/ and print "$main_url\n";

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Gunnar Hjalmarsson

2004-11-25, 3:55 am

Gunnar Hjalmarsson wrote:
> Brian Volk wrote:
>
> You want to remove the newline character:
>
> chomp;
>
>
> You probably mean:
>
> $main_url =~ s/1804/$_/ and print "$main_url\n";


Hmm.. That suggestion wasn't very clever, I think. $main_url needs to be
unchanged, and the substitution needs to take place twice per number.
Try this instead:

print "$main_url\n";
while (<KCITEM> ) {
chomp;
my $new_url;
( $new_url = $main_url ) =~ s/1804/$_/g and print "$new_url\n";
}

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com