setting range of indices in an array with a constant value
Question posted by: zcabeli
(Newbie)
on
May 11th, 2008 11:00 AM
Hi
i'd like to set a range of indices in a list with one constant value.
for example
if list = [ 1 2 3 4 5 6]
then
list(1..4,6) = 9
will change 'list' as so it now equals = [ 9 9 9 9 5 9]
i tried $list(1..4) = 9 but it only set the first index cell
how this action can be done ?
thanks,
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
|
|
May 12th, 2008 05:16 AM
# 2
|
Re: setting range of indices in an array with a constant value
Quote:
Originally Posted by zcabeli
Hi
i'd like to set a range of indices in a list with one constant value.
for example
if list = [ 1 2 3 4 5 6]
then
list(1..4,6) = 9
will change 'list' as so it now equals = [ 9 9 9 9 5 9]
i tried $list(1..4) = 9 but it only set the first index cell
how this action can be done ?
thanks,
|
This is one way of doing it:
Code: ( text )
use Data::Dumper; @list=(1,2,3,4,5,6); foreach my $el ((1..4,6)) { #find the index of the element to br replaced my ($index) = grep ($list[$_] eq $el , 0 .. $#list); splice(@list,$index,1,9); #replace one element at that position with 9 } print Dumper @list;
Not the answer you were looking for? Post your question . . .
170,099 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).
|
|
|
Top Perl Forum Contributors
|