<br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><div style="font-family: courier new,monospace;" class="gmail_quote">On Wed, Jun 30, 2010 at 11:41 AM, Shlomi Fish <span dir="ltr"><<a href="mailto:shlomif@iglu.org.il">shlomif@iglu.org.il</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Hi Fulko,<br>
<div class="im"><br>
On Wednesday 30 Jun 2010 18:06:49 Fulko Hew wrote:<br>
> As the subject line asks...<br>
> what I'd like to do is something like<br>
><br>
> foreach (@array) {<br>
> if (condition) {<br>
> splice @array, this_entry, 1;<br>
> next;<br>
> }<br>
> do processing on this entry;<br>
> }<br>
><br>
> @array now contains a sub-set of the original<br>
> that I can now further process<br>
><br>
<br>
</div>It's always a bad idea to modify the order of an array's elements while<br>
iterating over it using foreach. Don't do that.<br></blockquote><div><br>OK, let me give you the specific example...<br>The array contains all of the lines of a file...<br>The foreach is used to loop over then, and 'take action' upon them.<br>
But when I'm done I want all the empty lines stripped from the array ('just because!')<br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I should also note splicing an index out of the middle of an array is an O(N)<br>
operation where N is the number of elements in the array.<br><div class="im"></div></blockquote><div><br>I know, thats why I was looking for a 'faster' technique.<br><br>I guess the alternative could be (shown simplified):<br>
<br>my @array;<br>while ($_ = <>) {<br> next unless $_; # skip empty lines<br> push @array, $_; # but remember non-empty lines<br> process($_);<br>}<br> </div></div><br style="font-family: courier new,monospace;">