in reply to removing an element without index

Since you didn't provide much detail (or code), I'm making some assumptions on what you're trying to do. The example code below will "remove an element from an array without knowing its index" provided that you know what that element's value is. It may not be elegant or efficient, but it will do what you're wanting.

use strict; my @data = (1,2,3,4,5,6,7,8,9,10); my (@temp) = (@data); # copy array into a temporary array undef @data; # wipeout initial array foreach my $item (@temp) { if ($item !~ m/4/) { # if the element's value is not 4 push @data, ($item); # copy it back into the array } # otherwise do nothing }

Replies are listed 'Best First'.
Re^2: removing an element without index
by GrandFather (Saint) on Aug 10, 2010 at 00:47 UTC

    That is a little more succinct as:

    my @data = (1,2,3,4,5,6,7,8,9,10); @data = grep {! m/4/} @data; # Remove elements containing the digit 4
    True laziness is hard work

      This would be as succinct, more accurate, and a bit faster:

      @data = grep $_ ne '4', @data; # Remove elements equal to '4'

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        BrowserUk:

        It's not equivalent, though. I don't know if it would suit Levex's requirements or not, but it would give different results than the parent post if 14 or "BA4" were in the list of data.

        ...roboticus

Re^2: removing an element without index
by AnomalousMonk (Archbishop) on Aug 10, 2010 at 00:01 UTC
    if ($item !~ m/4/) { ... }

    "If the scalar  $item after stringization does not have a '4' digit anywhere in it, then do ..." would be a better description. E.g., if it's not 4, 44, 3.14159, etc.