View Full Version : variable changes in foreach loop, but I need it to stay changed.
Super Sonic
05-14-2004, 01:54 AM
I have a foreach loop. Every time it loops over, a variable is increased by one. But then the next time it loops, the variable had been reset to its original state before the loop. I need the variable that is incremented in the loop to stay so even outside the loop.
some code segment showing what you are trying to do and what's happening would be useful.
Super Sonic
05-14-2004, 02:23 AM
foreach (@UPLOADS) {
$upload_counter++;
my $out_filename = "FullShot$upload_counter";
open (OUTFILE, ">$out_filename");
while ($bytesread = read ($_, $buffer, 1024)) {
print OUTFILE $buffer;
}
close $_;
Close OUTFILE;
}
Notice that each time the foreach loop runs, $upload is increased. Each time a new element goes through the loop, $upload_counter is reset to how it was before the loop occured (that's just perl). Now, I'm using 5 upload fields. @UPLOADS is an array of the variables storing the names of the files to uploaded. Each of the five files will save to a file 'FullShot(1-5)'
At the moment, when It runs, it successfully saves one upload to FullShot1, the rest of them are saved to CGITEMP files.
dswimboy
05-14-2004, 02:59 AM
trying setting $upload_counter equal to one, before your loop.
or rather zero, since you're incrementing it first.
Super Sonic
05-14-2004, 03:15 AM
I just tried.
:( same thing happens.
dswimboy
05-15-2004, 09:06 PM
try this code:
foreach $upload (@UPLOADS) {
$upload_counter++;
$out_filename = "FullShot$upload_counter";
open (OUTFILE, ">$out_filename");
open (INFILE, "<$upload");
while ($bytesread = read(INFILE, $buffer, 1024)) {
print OUTFILE $buffer;
}
close INFILE;
close OUTFILE;
}
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.