in reply to Re^2: How can I delete an element in a foreach cycle?
in thread How can I delete an element in a foreach cycle?

It's never a good idea to mess with the JavaFan, but would either of those approaches be faster or more straightforward than, or in any way preferable to something like (assuming the array may contain undefined elements):

>perl -wMstrict -le "my @array = (qw(hi hiya hello hello), undef, qw(lo hello)); @array = grep !defined || $_ ne 'hello', @array; print defined($_) ? qq{'$_'} : 'UNDEF' for @array; " 'hi' 'hiya' UNDEF 'lo'

Replies are listed 'Best First'.
Re^4: How can I delete an element in a foreach cycle?
by saintex (Scribe) on Feb 27, 2010 at 07:32 UTC
    The JavaFan code works for sure, but it looks like c/c++ way: incrementing a number, you've got the index reference... but it doesn't appear too pearlish. There is a way to do it in a more perlish way? AnomalousMonk, I don't understand perfectly your code. You see, I'm only a novice. Please, Can you do a better explains?
      @array = grep !defined || $_ ne 'hello', @array;

      The grep built-in function takes a list (in this case supplied by @array) and returns only those elements of the list for which the BLOCK or EXPRESSION of the function evaluates true. In this case, the expression is
          !defined || $_ ne 'hello'
      and only elements of the input list that are either not defined or not string-equal to 'hello' are output. A better name for the  grep function might be 'filter'.

      One of the mottos of Perl is "there's more than one way of doing it". Thinking one way is "more Perlish" than another is very unPerlish.

      If you want to eliminate everything that Perl borrowed/copied from C, you're left with an utterly useless language not many people will use.

Re^4: How can I delete an element in a foreach cycle?
by JavaFan (Canon) on Feb 27, 2010 at 16:42 UTC
    Well, the question was how to delete from a foreach cycle; a grep isn't one. I presumed the "delete if equal to a specific value" was an example. I prefer
    foreach (...) { multiple lines of statements }
    over
    @array = grep { multiple lines of statements } @array;
Re^4: How can I delete an element in a foreach cycle?
by saintex (Scribe) on Feb 27, 2010 at 18:04 UTC
    ok, thank you all for your answers. I like that way: to mark the element as "undef" in foreach cycle and then delete it throught a grep. I understood also the lesson of JavaFan: "there is more than one way to do it". But I'm also searching a way for me. That way must be a little different from the way I usually know (like the construct similar to C/C++), just because from my personal point of view (this is only my vision) I'm using perl just to have shorcuts where other programming languages are using explict constructs. These shortcuts for me are the funny part of perl. Thank you for all the answers.
      ... mark the element as "undef" in foreach cycle and then delete it throught [sic] a grep.

      There are indeed many paths in programming as in life, and if you are happy with the one you have chosen, then so, too, I, and wish you well of it.

      But I must say I don't understand your allegiance to for(each). The only reason I can see for using it is to process the array 'in-place': grep will (I think; haven't researched or tested this) create a temporary array, thus possibly effectively doubling the size of the array being processed; thus, if the array just fits into available memory to begin with, you will run into problems.

      But if you're going to use a  grep also, why not just use a  grep period? (However, this question is just idle curiosity.)

Re^6: How can I delete an element in a foreach cycle?
by saintex (Scribe) on Mar 02, 2010 at 08:48 UTC

    Anomalous Monk, the reason I used "throught" is that I'm not a "natural English speraker"... so my thoughts maybe inaccurate in English...

    ...and yes, I can use grep at all, instead of use a foreach, and then a grep.

    Thank you for your answers!

      Sorry, didn't mean to seem censorious. Just wanted to follow the good practice of flagging any oddity in a quote to show it has been copied exactly from the original.