in reply to Splice not working or is it me?

You are all right. It is me. I've discovered my problem:

while ($x <= @array){ if ($array[$x] =~ m/$regex/){ splice @array, $x, 1; } }

The problem is that if I remove, for example, element 4 now element 5 becomes element 4 and therefore it is never checked. How can I prevent this?

Neil Watson
watson-wilson.ca

Replies are listed 'Best First'.
Re: Re: Splice not working or is it me?
by japhy (Canon) on Sep 14, 2002 at 16:42 UTC
    Use grep(): @array = grep !/$regex/, @array; Otherwise, with your while loop:
    $x = 0; while ($x < @array) { # NOT <=, but < if ($array[$x] =~ /$regex/) { splice @array, $x, 1 } else { ++$x } }
    That way you only increment if a match wasn't found.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;