If you want to collect all date values, you will need an array, not a scalar:
my @dates; foreach $odate (@logs) { if ($odate =~ /\bdate=(\d{1,4}-\d{1,2}-\d{1,2})/i) { push @dates, $1; print "Found $1 as a new date\n"; } } print "I collected the following dates: @dates\n"; for my $date (@dates) { print "Processing $date\n"; };
Also, while the following code works, it could be written differently:
while (@logs = <FILE>) { foreach (@logs) { ...
The above code reads the contents of FILE into @logs in one go, and then iterates over that list one by one. You can roll the two "loops" into one by eliminating @logs and using:
while (my $odate = <FILE>) { ... };
In reply to Re: Scalar looses values outside foreach loop
by Corion
in thread Scalar looses values outside foreach loop
by cipher
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |