slice of multidimensional array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave Bazell

    slice of multidimensional array

    I have a multidimensiona l array, say 20 x 6, and I want to take a subset of
    the rows. I have an index array
    @indx=(1,3,12,1 7) 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
    Re: slice of multidimensiona l array

    OK, Thanks, that clarifies some things. However, I want to repeatedly slice
    my multidimentiona l 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.inv alid> wrote in message
    news:avETa.5720 8$EZ2.43173@nwr ddc01.gnilink.n et...[color=blue]
    > "Dave Bazell" <bazell@comcast .net> wrote in message
    > news:2oacnZxyuq B9fIOiU-KYvw@comcast.co m...[color=green]
    > > @indx=(1,3,12,1 7) 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?[/color]
    >
    > 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[/color]
    @data[color=blue]
    > below to @$data.
    >
    > # setup
    > $first = 'a0';
    > push @data, [map {$first++} 0..9] for 1..26;
    > @indx=(1,3,12,1 7)
    > @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
    >
    >[/color]


    Comment

    • dw

      #3
      Re: slice of multidimensiona l array


      "Dave Bazell" <bazell@comcast .net> wrote in message
      news:qtecnWnmpa J8qL2iXTWJjw@co mcast.com...[color=blue]
      > OK, Thanks, that clarifies some things. However, I want to repeatedly[/color]
      slice[color=blue]
      > my multidimentiona l array and append the slices to a new array. Thus I[/color]
      can[color=blue]
      > do:
      >
      > @array_slice = @data[@indx];
      > This works fine. If I look in the debugger I can see the array elements:[/color]
      p[color=blue]
      > $tmp[0][0] = 18.392
      >
      > Now I try to append to another array:
      >
      > push (@new_data, [@array_slice]);[/color]
      [@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;
      [color=blue]
      >
      > This does not seem to work. In the debugger I see:
      > p $new_data[0][0] is blank;[/color]
      What was in @new_data to begin with, how did you initialize it?
      [color=blue]
      > p $new_data[1][0] = ARRAY(0x8272cac )[/color]
      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.




      Comment

      Working...