[EMAIL PROTECTED] (Adam Stern) wrote:
>Also, another question.  I know perl supports array slicing, but how
>advanced does it get into it.  Can it do extensive stuff like Fortran
>shown in this picture:
>http://bingweb.binghamton.edu/~head/CS471/NOTES/RUNTIME/2slice.gif

There's no built-in fanciness of the specific types shown there, but
there are always slice tricks, sometimes involving map():

Let @matrix be defined by:

@matrix = ([ 1, 2, 3, 4, 5, 6, 7, 8, 9,10],
           [ 2, 3, 4, 5, 6, 7, 8, 9,10,11],
           [ 3, 4, 5, 6, 7, 8, 9,10,11,12],
           [ 4, 5, 6, 7, 8, 9,10,11,12,13],
           [ 5, 6, 7, 8, 9,10,11,12,13,14],
           [ 6, 7, 8, 9,10,11,12,13,14,15],
           [ 7, 8, 9,10,11,12,13,14,15,16],
           [ 8, 9,10,11,12,13,14,15,16,17],
           [ 9,10,11,12,13,14,15,16,17,18],
           [10,11,12,13,14,15,16,17,18,19],
          );

Then the first example is:

  @slice = map {[@$_[2..5]]} @matrix[3..6];
    or
  @slice = map {\@$_[2..5]}  @matrix[3..6];

Second example (assuming you want it one-dimensional, and not a 1x5
matrix):

  @slice = @{$matrix[4]}[5..9];

Third example:

  @slice = map {[@$_[0..3]]} @matrix[1,3,5,7];

Fourth example:

  @slice = @matrix[1,4,8];


I suspect that the fourth example is the only one you'll be truly
satisfied with. =)

Note that you can always extend data structures with different methods,
via tie() and/or overloading.  So you could implement a richer slicing
behavior if you can think of how the interface should look.  Then you
could even package it up as a module to share with others. 


  -------------------                            -------------------
  Ken Williams                             Last Bastion of Euclidity
  [EMAIL PROTECTED]                            The Math Forum

Reply via email to