On 12-03-13 12:11 PM, oxy wrote:
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).
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.
Reset $thevariable before the loop and exit the loop when the first
variable1 is found:
# make sure $thevariable is undefined
my $thevariable = undef;
# read the list of files on the command line or STDIN
READ_ARG_LIST:
while( my $line = <> ){
# check for the tag "variable1"
if( $line =~ /variable1=(.*)/ ){
# found it; save its value
$thevariable = $1;
# exit the while loop
last READ_ARG_LIST;
} # end if
} # end while
--
Just my 0.00000002 million dollars worth,
Shawn
Programming is as much about organization and communication
as it is about coding.
It's Mutual Aid, not fierce competition, that's the dominate
force of evolution. Of course, anyone who has worked in
open source already knows this.
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/