By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,827 Members | 2,244 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,827 IT Pros & Developers. It's quick & easy.

slice of multidimensional array

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: 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" <me@verizon.invalid> wrote in message
news:av*******************@nwrddc01.gnilink.net...
"Dave Bazell" <ba****@comcast.net> wrote in message
news:2o********************@comcast.com...
@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

Jul 19 '05 #2

P: n/a
dw

"Dave Bazell" <ba****@comcast.net> wrote in message
news:qt********************@comcast.com...
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.


Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.