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
    Thanks for the quick reponse Corion!
    Can I push these array entries in mysql table ?
    I know for scalar it works, Also when I print array it shows output side by side even after provinding \n.

      What do you mean by "Can I push these array entries in mysql table ?" ? Do you want to write the information into a Mysql database table? If so, you need to write them one by one.

      For outputting the array, you can either interpolate it as I showed you, or print it item by item, as I also showed you.