Deleting elements of an array from a particular index to the end till there is a valid element
Hi i have this array say
name[0] = hello
nam开发者_Go百科e[1] = bye
name[2] = ok
name[3] = wassup
and so on....
i want to pop the elements from the index 2 to the end of the array because i want to replace them with new ones.I am running a loop so cant keep track of the individual indices but would be able to keep track of the element 2.So how could i pop the elements from 2 onwards to the rest till the array is defines ?
i was using
delete array[2,..]
is this valid?
This is what the splice function is for; just do splice @array, 2;
.
Assign an array slice to itself:
@array = @array[0,1];
my @array = ( ... stuff ...);
@array = @array[0 .. 1];
Just take what you want and leave the rest.
You might be considering matching against a certain array value:
my @array = ( stuff );
my @rest;
for (0 .. $#array) {
if ($array[$_] =~ /^ok$/) { push @rest, @array[$_ .. $#array]; last }
}
精彩评论