in reply to Deleting Elements from an Array without their Index Number?

Do you want to remove coin from the array?

my @not_coin = grep { $_ ne 'coin' } @array;

Or do you want to replace coin by undef?

my @not_coin = map { $_ eq 'coin' ? undef : $_ } @array;

Replies are listed 'Best First'.
Re^2: Deleting Elements from an Array without their Index Number?
by Fletch (Bishop) on Jul 23, 2006 at 23:58 UTC

    And of course you could also use grep to determine the index if you later would like to use it to remove elements with splice.

    my @coin_idx = grep { $array[ $_ ] eq 'coin' } 0..$#array;
Re^2: Deleting Elements from an Array without their Index Number?
by diotalevi (Canon) on Jul 24, 2006 at 00:32 UTC

    I like how writing these as map+ternary makes it obvious that grep is just a specialization of map.

    my @not_coin = map { $_ eq 'coin' ? () : $_} @array; # Or do you want to replace coin by undef? my @not_coin = map { $_ eq 'coin' ? undef : $_} @array;

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

      And map is a specialization of foreach+push.
      my @not_coin; foreach (@array) { push(@array, $_ eq 'coin' ? () : $_); }
      How does this help?

        How does this help? It's always good to know the more general form of your specializations. Knowing that grep is map+flat list, you can turn chained grep/map into single expressions to avoid traversing your entire list multiple times, once for each step in the chain.

        ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊