![]() |
|
|
|||||||
![]() |
PERL - slice of multidimensional array |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
I have a multidimensional array, say 20 x 6, and I want to take a subset of
the rows. I have an index array @indx=(1,3,12,17) for example, and what I want to say is @new_array = $data[@indx][0..5] I know that does not work. It has something to do with using the appropriate reference to the array elements. How should I do this? And why does it work? Thanks, Dave Dave Bazell |
|
|
|
|
#2 |
|
Posts: n/a
|
OK, Thanks, that clarifies some things. However, I want to repeatedly slice
my multidimentional array and append the slices to a new array. Thus I can do: @array_slice = @data[@indx]; This works fine. If I look in the debugger I can see the array elements: p $tmp[0][0] = 18.392 Now I try to append to another array: push (@new_data, [@array_slice]); This does not seem to work. In the debugger I see: p $new_data[0][0] is blank; p $new_data[1][0] = ARRAY(0x8272cac) I guess I have to dereference the @array_slice differently to get it into a new array? Thanks for the help. Dave "dw" <> wrote in message news:avETa.57208$... > "Dave Bazell" <> wrote in message > news:2oacnZxyuqB9fIOiU-... > > @indx=(1,3,12,17) for example, and what I want to say is > > > > @new_array = $data[@indx][0..5] > > > > How should I do this? And why does it work? > > To get a slice, you need to use @data[@indx] (note the @ instead of $). > This will give you the selected rows and you don't need [0..5] at the end. > However, if you have 26x10 and you only want 4x6, then you can't do it > directly. My example below uses map. > > If you are using an array ref $data then change all the occurrences of @data > below to @$data. > > # setup > $first = 'a0'; > push @data, [map {$first++} 0..9] for 1..26; > @indx=(1,3,12,17) > @indx2=(0..5); > > # This gives you a subset of the rows > # Use @data instead of $data since you want a slice. > @new_array = @data[@indx]; > > # This gives you a subset of rows, and then a subset of the columns > @selective_cols = map {[@$_[@indx2]]} @data[@indx]; > > dw > > |
|
|
|
#3 |
|
Posts: n/a
|
"Dave Bazell" <> wrote in message news:... > OK, Thanks, that clarifies some things. However, I want to repeatedly slice > my multidimentional array and append the slices to a new array. Thus I can > do: > > @array_slice = @data[@indx]; > This works fine. If I look in the debugger I can see the array elements: p > $tmp[0][0] = 18.392 > > Now I try to append to another array: > > push (@new_data, [@array_slice]); [@array_slice] will create a new array reference with the elements of @array_slice in it. Drop the brackets and I think you will get what you want: push @new_data, @array_slice; > > This does not seem to work. In the debugger I see: > p $new_data[0][0] is blank; What was in @new_data to begin with, how did you initialize it? > p $new_data[1][0] = ARRAY(0x8272cac) This is most likely because of the brackets you used around @array_slice. I'm a little confused here: you start out assigning @array_slice but print out data from @tmp in the debugger. |
|