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

Hi fellow monks,

I am currently sitting here and am trying to find out what I am doing wrong.

I have an array in which some slices might end with '='. In these cases Id like to join the next slice to the current. I am currently using splice in order to remove slices from my array. For a strange reason I am losing some parts of my array. Can you spot what I am doing wrong?

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @mail = ('Hi i a=','m a cont=','inous &lt;tex=','t&gt; Stop','I ha= +','ve no','idea w=','hats','wrong','grr'); my $i = 0; my $y = 0; while ($mail[$i]) { #print Dumper \@mail, $i; $y = $i + 1; while ($mail[$i] =~ s/=$//) { $mail[$i] .= $mail[$y]; my @tmp = splice (@mail,$y,$y); print Dumper \@tmp; } $mail[$i] =~ s/&lt;/</g; #Some other stuff I need to do $mail[$i] =~ s/&gt;/>/g; $i++; } print Dumper \@mail;
The result should look like this
$VAR1 = [ 'Hi i am a contnous <text> Stop', 'I ha've no', 'idea whats', 'wrong', 'grr' ];
But in fact Ill get:
$VAR1 = [ 'Hi i am a continous <text> Stop', 'I have no', 'hats', 'wrong', 'grr' ];
I would be probably be quicker if I join the array to a big string, remove every '=\n' and exchange my '><' and push everything back into a new array. But I am now stuck with splice as I dont see what my error is.
Hope you can push me into the right direction :)

Thx

Schuk

Replies are listed 'Best First'.
Re: splice n dice
by Paladin (Vicar) on Mar 09, 2006 at 18:12 UTC
    Splice is:
    splice ARRAY, OFFSET, LENGTH
    You only want to splice 1 element out, not $y elements.
Re: splice n dice
by ivancho (Hermit) on Mar 09, 2006 at 18:14 UTC
     splice (@mail,$y,$y) should be  splice (@mail,$y,1), no?
      doh.. ok thanks... that would be my problem.
      Not reading the docs properly ;-)

      Cheers

      Schuk