What exactly are you doing in "doSomething", "doSomething", and "_call"? Can you show us some working code that illustrates the problem, i.e. code we can download and run (without having to install the world as dependencies)? If this is happening at all it certainly isn't happening for every possible "doSomething" or "doSomethingElse" or "_call". Perl regularly handles very large arrays in for loops. For example, the following code shows no evidence of wierd behavior for arrays with 1000+ elements.

use strict; use warnings; sub doSomething { #taint much but its something my $x=<<__EOF__ In the merry month of may The fields are decked with flowers gay Fa la la la, fa la la la, fa la la la, fa la la la The woods and groves with birds do sing Redoubling echos as they sing Fa la la la, fa la la la, fa la la la, fa la la la (Youll, 1608, #19) __EOF__ } sub doSomethingElse { # this is even less my $x=1+2; } { package Thingy; sub makeACall { my ($self, $addend) = @_; $$self += $addend; } } #---------------------------------------- my $N=scalar(@ARGV)?$ARGV[0] : 1000; my @array = (0..$N); my $incremented=0; my $self = bless(\$incremented, 'Thingy'); foreach (@array){ doSomething $self->makeACall($_); doSomethingElse } my $calculated = ($N*($N+1))/2; #operations ordered to avoid oddNum/2 print "added via loop: <$incremented> added via formula: <$calculated> +\n"; #--------------------------------------- # why it is a *bad* idea to remove elements from an array while loopin +g $$self = 0; #reset incremented; print "starting calc: $incremented\n"; foreach (@array) { doSomething $self->makeACall($_); shift @array; # get rid element, we don't need it anymore do we? doSomethingElse } # run this with even a measely 6 elements ($N=6) and you will see that # $incremented just doesn't add up to what it should print "deleting elements after use ...\n"; print "added via loop: <$incremented> added via formula: <$calculated> +\n"; # # So why did we get 12 instead of 21? # - we deleted the element at the front of the array # - that moves whatever is at $array[1] to $array[0] # - ***the foreach loop doesn't know that we did that*** # - Perl goes to the next element (i.e. 1) # - what happens? only every other element in the original array # is visited: 0 + 2 + 4 + 6 = 12. #

Absent any other information, I would guess one of two things:

Update: In response to OP's update, I have added code to illustrate why deleting elements from a loop you are currently iterating through is a bad idea (Perl will let you do it, but the foreach loop isn't expecting it).

Update2: (3:15 IST) Added an explanation of why deleting elements from arrays can cause loops to process elements in unexpected ways - see both immediately above and in code sample.

Best, beth


In reply to Re: foreach loop - could it beeeeee!! by ELISHEVA
in thread foreach loop - could it beeeeee!! by baxy77bax

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.