473,394 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

slice of multidimensional array

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
2 8010
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Michael Ruebner | last post by:
Greetings, From a DB query, I receive the following result set: array(array('key1'=>'some val', 'key2'=>'some other val', ..., n), array('key1'=>'some val', 'key2'=>'some other val', ..., n),...
15
by: Roberto A. F. De Almeida | last post by:
I found that when using negative indices, the slice object passed to __getitem__ depends on the number of slices. An example to clarify: class a: def __getitem__(self, index): return index ...
0
by: Rich Krauter | last post by:
I do not understand why python behaves the way it does in the following code: Here, Python doesn't like that I'm assigning out-of-range. OK. That's fine. >>> x = >>> x = 3 Traceback (most...
4
by: F. Da Costa | last post by:
Hi, I was wondering whether someone could enlighten me as to the reason why the slice does not work in IE when the arr is passed in properly. Checked the values in the srcArr and they are...
9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
7
by: RubyRedRick | last post by:
I bought Crockford's "JavaScript: The Good Parts" yesterday to help build my JavaScript foo. On page 44, he gives an implementation of the curry function: Function.method('curry', function() {...
12
by: lorlarz | last post by:
In the code sample below, how are arguments a legitimate argument to Array.slice? Function.prototype.bind = function(){ var fn = this, args = Array.prototype.slice.call(arguments), object =...
9
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.