jonnyfolk has asked for the wisdom of the Perl Monks concerning the following question:
(I have left in my debugging print statements.)
#!/usr/bin/perl -w use strict; use CGI ':standard'; my $data="/path/data_add.txt"; my $checkthree = param('check'); my $num = param('num'); my $action = param('action'); my @line1 = (); my @line_no = (); my ($one,$two,$three,$four,$five); print header(), start_html; open (FILE,"$data") || die "whoops: $!"; while (my $line =<FILE>) { ($one,$two,$three,$four,$five) = split "\t",$line; if (($action eq 'Put') && ($three eq $checkthree)) { $five = $five + $num; print "we have $five on line $.<br>"; @line1 = ($one,$two,$three,$four,$five); push (my @line_no, $.); print "Did this work @line_no<br>"; last; } elsif (($action eq 'Take') && ($three eq $checkthree)) { $five = $five - $num; @line1 = ($one,$two,$three,$four,$five); push (@line_no, $.); last; } } close FILE; if ($five < 0) { print "message: can't do this"; } else { my $line1 = join "\t", @line1; print "taking it out of the loop we have @line_no<br>"; my $line_no = join " ", @line_no; print "we have $five on line $line_no<br>"; open (FILE,"$data") || die "whoops 1: $!"; my @all=<FILE>; close (FILE); $all[$line_no-1] = $line1; open (FILE2,">$data.tmp") || die "whoops 2: $!"; foreach my $line (@all){ $line=~s/\n//g; print FILE2 "$line\n";} close(FILE2); rename("$data.tmp", "$data") || die "whoops 3: $!"; print "the field now contains: $five"; } print end_html();
My first question is:
having declared
I had expected both values to exit the loop - only @line1 does.my @line1 = (); my @line_no = ();
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Picking up information coming out of a loop
by slife (Scribe) on Dec 12, 2002 at 12:39 UTC | |
|
Re: Picking up information coming out of a loop
by Abigail-II (Bishop) on Dec 12, 2002 at 12:42 UTC | |
by jonnyfolk (Vicar) on Dec 12, 2002 at 14:58 UTC | |
by jdporter (Paladin) on Dec 12, 2002 at 17:24 UTC | |
by slife (Scribe) on Dec 12, 2002 at 15:07 UTC |