slice of multidimensional array

D

Dave Bazell

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
 
D

Dave Bazell

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 said:
Dave Bazell said:
@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
 
D

dw

Dave Bazell said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top