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