On 3/13/12 Tue Mar 13, 2012 9:11 AM, "oxy" <oxyo...@googlemail.com> scribbled:
> hi all, > > I have a problem with the following structure: > > while(<file>){$thevariable=$1 if (/variable1=(.*)/)}; > > Now I wanna be sure that variable1 was really set in the above > statement (it could have an old value from a previous embracing loop). You could reset $thevariable before entering the loop: undef $thevariable; > Then I tried: > > while(<file>){if (/variable1=(.*)/) > {$thevariable=$1} else {$thevariable="nonset"}; > > The problem is, the second while statement is resetting $thevariable > for every line in the file and not only for the matching one. The > else {$thevariable="notset"} option should be actually only run if > we reach the end without matching. But both while structures above > aren't solving my problem. Put a print statement in the true branch of the if statement so you can tell if and when the match succeeds and $thevariable is set. The variable will be reset if there are any non-matching lines following a line that matches, so you will need to avoid doing that. You can reset the variable to undef before the loop, set it only if a line matches, and check for definedness after the loop to see if it was set: if( defined $thevariable ) { print "Variable was set to $thevariable\n"; }else{ print "No match\n"; } For more help, you should post a short, complete program, including some typical data lines, that demonstrates the problem you are having. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/