Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Replacing with a variable in while loop

by tekio (Novice)
on May 01, 2011 at 00:45 UTC ( [id://902277]=perlquestion: print w/replies, xml ) Need Help??

tekio has asked for the wisdom of the Perl Monks concerning the following question:

Okay, I'm adding to a fairly large script that is already in use at my company. What I want to do is a "one liner", here.
while($var = <VAR>){ chomp($_); #change token in url query string to $var $urlChop[1] =~ s/MATCH/$_/;
The problem I'm having (well, besides being new to Perl.) Is it only updates to the first instance of $var in the loop. Basically it is setting a POST parameter in the URL to something from a text file. ie:
http://someurl.com/some.php?id=1337&product=$var

Replies are listed 'Best First'.
Re: Replacing with a variable in while loop
by wind (Priest) on May 01, 2011 at 00:56 UTC
    Do you just need global replacement?
    $urlChop[1] =~ s/MATCH/$_/g;
Re: Replacing with a variable in while loop
by cdarke (Prior) on May 01, 2011 at 06:41 UTC
    You don't show any code where $var is used, other than when reading a record from the file. The chomp and the s are using $_, not $var. Is that correct?

    Maybe you could show the rest of the loop, or at least where you use $var.
      I've tried using both $var and $_, as $_ should represent $var in this example. My question pertains to this example, guess I should have mentioned the first line in the example:
      open(VAR, "<theinputfile.txt");
      The entire script is well over 500 lines, and the mentioned snippet is an example of where I'm having problems. As well as I could probably loose my job for posting company source code on the Interwebz. I was just wondering why $var (or $_ which is equal to $var in this scenario) is not updating with each iteration of the while loop. I've done similar things with both Pascal and PHP. It just bewilders me why, in the given example, the variable will not update with each iteration of the loop. Obviously, it is because it is being used as a substitution. I was really just trying to understand the logic behind why. I've figured out a solution to the problem, using another method. But I'd still like to know the logic behind, the why.
Re: Replacing with a variable in while loop
by Mr. Muskrat (Canon) on May 02, 2011 at 20:38 UTC

    As the others have already said, your code doesn't work because you aren't reading anything into $_. (You're reading into $var but using $_ later.)

    Try this instead:

    open(my $fh, '<', 'theinputfile.txt') or die "Unable to open theinputf +ile.txt, $!\n"; while(my $var = <$fh>) { # Reading each line one by one from theinputf +ile.txt into $var chomp($var); # chomp $var not $_ since $_ isn't used #change token in url query string to $var $urlChop[1] =~ s/MATCH/$var/; # replace with $var not $_ since $_ isn +'t used # ... whatever else you need to do }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://902277]
Approved by LanX
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-23 19:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found