in reply to Scalar looses values outside foreach loop
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>) { ... };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Scalar looses values outside foreach loop
by cipher (Acolyte) on Dec 02, 2010 at 09:41 UTC | |
by Corion (Patriarch) on Dec 02, 2010 at 10:08 UTC |