neilwatson has asked for the wisdom of the Perl Monks concerning the following question:

Consider this code:

#!/usr/bin/perl -w use strict; use warnings; my @array = qw(part1 part2 part3 part4 part5 part6 part7 part8 ); #remove part4 splice @array, 5, 1; print @array;

Now consider the output:

part1part2part3part4part5part7part8

Should part4 not have been removed from @array? I'm using perl 5.6.1.

Neil Watson
watson-wilson.ca

Replies are listed 'Best First'.
Re: Splice not working or is it me?
by George_Sherston (Vicar) on Sep 14, 2002 at 12:34 UTC
    #!/usr/bin/perl -w use strict; use warnings; my @array = qw( part1 part2 part3 part4 part5 part6 part7 part8 ); #remove part4 - which is $array[3], not $array[5]! splice @array, 3, 1; print @array;
    BTW, does anyone else familiar with rope-making and sailing think that splice is exactly the wrong name for this function?

    § George Sherston

      Could it be referring to the 4 argument version of splice and that when you splice a rope to make a fixed loop for instance, you have to squeeze one part of the splitted rope back inbetween the rope itself??

      Just my 2 öre

Re: Splice not working or is it me?
by broquaint (Abbot) on Sep 14, 2002 at 12:39 UTC
    That works as expected
    # 0 1 2 3 4 5 6 7 my @array = qw(part1 part2 part3 part4 part5 part6 part7 part8); print splice(@array, 5, 1), $/; print @array; __output__ part6 part1part2part3part4part5part7part8
    As the docs for splice say, it Removes the elements designated by OFFSET and LENGTH from an array, and replaces them with the elements of LIST, if any.
    HTH

    _________
    broquaint

Re: Splice not working or is it me?
by kabel (Chaplain) on Sep 14, 2002 at 12:37 UTC
    the output is perfectly right. you counted the wrong direction ;)
    name => elem. no part1 => 0 part2 => 1 part3 => 2 part4 => 3 part5 => 4 part6 => 5 part7 => 6 part8 => 7
    splice @array, 5, 1 is translated: get one element from the fifth element on out of the array @array (and replace it with nothing)
Re: Splice not working or is it me?
by particle (Vicar) on Sep 14, 2002 at 17:05 UTC
    noone's mentioned it yet, so i'll ask... why are you using -w and use warnings?

    use -w for backward compatibility. require 5.006; use warnings; for new code. don't use both, you'll get into trouble that way. read perllexwarn for more -- specifically What's wrong with -w and $^W.

    ~Particle *accelerates*

Re: Splice not working or is it me?
by neilwatson (Priest) on Sep 14, 2002 at 16:35 UTC
    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

      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:??;