Offer Kaye                                                    
             <[EMAIL PROTECTED]                                             
             .com>                                                      To 
                                       Perl Beginners <beginners@perl.org> 
             08/09/2005 01:31                                           cc 
             PM                                                            
                                                                   Subject 
                                       Re: popping, shifting or splicing   
                                       an array???                         
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           







On 8/9/05, DBSMITH wrote:
>
> Hello ...
>
> I am trying to figure out, which operator to use; pop, shift or splice.
> My goal is,  as an option is selected from user input,  pop or splice or
> shift elements off ( those selected from user input ) and repopulate
array
> with remaining elements.
>

Hi,
First of all, I don't grok your "splice" examples. According to
'perldoc -f splice' (http://perldoc.perl.org/functions/splice.html),
the second argument to splice should be an offset - i.e. a number. So
"@numbers to remove" will be interpreted in scalar context, and the
offeset into @ArrayA will be the lengh of the list of numbers to
remove. Is that what you wanted?

A second point is that you use "shift" inside a "for" loop. That's *BAD*:
"If any part of LIST is an array, foreach  will get very confused if
you add or remove elements within the loop body, for example with
splice. So don't do that." (perldoc perlsyn)

"pop" removes that last value of an array (shortning the array).
"shift" also shortens the array, but by removing the first element.
Which one you want to use depends on what your array looks like and
what you want to do. In any case you don't need to "repopulate" the
array - both pop and shift change the array itself. Finally, note that
"splice" can 'emulate' both operators - see the "splice" perldoc page
for examples.

HTH,
--
Offer Kaye



++++++++++++++++++++++++++++++++++++++++++++++

thanks for the reply.. (>*<)

I probably need splice as I will need to delete multiple elements at the
same time
based on user selections.

(1..1000)
user selects 1..5
splice 1..5, returning new or existing array with 6..1000 as new elements.
Note each element contains unique data, etc. H#####

ok so my new for loop would be

foreach (splice @TEMPARRAY,scalar of @TEMPARRAY,scalar of @TEMPARRAY,)
{

}

I guess I am missing how Length and Offset differ in this context?






-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to