Hello.

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?


In reply to Array iteration: foreach works, for doesn't by D&#378;wiedziu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.