Dźwiedziu has asked for the wisdom of the Perl Monks concerning the following question:
In general my problem is that iterating trough an array using foreach works properly but using for and $awarie$iterator returns nothing.
If I didn't have to use two walues at one iteration of the last (for) loop, I would convert it to an foreach.
sub br {print "\n"}; sub parse_datetime($) { $_ =~ /(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d)/; return($1, $2, $3, $4, $5); } my @awarie = ('2009-11-13 13:48', '2009-11-13 16:58', '2009-11-13 19:23', '2009-11-13 23:13', '2009-11-16 15:31', '2009-11-16 21:41', '2009-11-17 16:21', '2009-11-17 21:06'); if ($DEBUG) { print('@awarie'); br; print Dumper(@awarie); print('parse_datetime($awarie[$i])'); br; #foreach(@awarie) # Works for(my $k=0; $k<scalar(@awarie); $k++) # Dosen't { #my ($y, $m, $d, $h, $mi) = parse_datetime($_); # ^^^^^^^^^^^^^^^^^^^^^^^^^ Works #my ($y, $m, $d, $h, $mi) = parse_datetime($awarie[$k]); # ^^^^^^^^^^^^^^^^^^^^^^^^^ Dosen't my ($y, $m, $d, $h, $mi) = parse_datetime($awarie[1]); # ^^^^^^^^^^^^^^^^^^^^^^^^^ Dosen't! print("$y-$m-$d $h:$mi"); br; } } if($DEBUG) { print('@array size: ' . scalar(@awarie)); br; # Everything is correct } for(my $j=0; $j<scalar(@awarie); $j++) { if($DEBUG) { print $awarie[$j] }; br; my ($yy, $mm, $dd, $phh, $pmi) = parse_datetime($awarie[$j++]); # ^^^ Nothing returned if($DEBUG) { print $awarie[$j] }; br; my ($kyy, $kmm, $kdd, $khh, $kmi) = parse_datetime($awarie[$j]); # ^^^ Ditto if($DEBUG) { print("$yy-$mm-$dd $phh:$pmi"); br; print("$kyy-$kmm-$kdd $khh:$kmi"); br; # Both print '--:'... } # (...) }
What am I missing?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Array iteration: foreach works, for doesn't
by moritz (Cardinal) on Nov 23, 2009 at 13:24 UTC | |
by Dźwiedziu (Initiate) on Nov 23, 2009 at 13:43 UTC | |
|
Re: Array iteration: foreach works, for doesn't
by Bloodnok (Vicar) on Nov 23, 2009 at 13:33 UTC |