Go Back   Velocity Reviews > Newsgroups > PERL
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

PERL - slice of multidimensional array

 
Thread Tools Search this Thread
Old 07-23-2003, 08:28 PM   #1
Default 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




Dave Bazell
  Reply With Quote
Old 07-24-2003, 08:39 PM   #2
Dave Bazell
 
Posts: n/a
Default Re: slice of multidimensional array

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
>
>



  Reply With Quote
Old 07-25-2003, 12:17 AM   #3
dw
 
Posts: n/a
Default Re: slice of multidimensional array


"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.




  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump