Re: question on reference to array slice



Rick wrote:

In C we can store a set of pointers which point to different arrays
into an array of pointer. This way we could fast access different
array by looking it up in this pointer array. Is there anyway to do
this in Perl. I've been trying different things and no luck so far.

LIke:
@nums = (1 .. 20);

I want to store the reference to @nums[1..10] into $data[0], and the
reference to @nums[11:20] into $data[1], so that I could visit, for
example, the second set of @nums by something like $data[1][5] to get
15.

$ perl -le'
@nums = 1 .. 20;
print "@nums";
$data[ 0 ] = [ \@nums[ 0 .. 9 ] ];
$data[ 1 ] = [ \@nums[ 10 .. 19 ] ];
print ${ $data[ 1 ][ 5 ] };
${ $data[ 1 ][ 5 ] } = 0;
print "@nums";
'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 17 18 19 20




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
.



Relevant Pages

  • Re: Library Design, f0dders nightmare.
    ... demo mistake but I suggest to you that a sequence of blunders on this ... Feed it through your parser to get the offset of each ... offset to its appropriate place in the array of pointers and when you ... dividing the byte count by two to determine the maximum pointer array ...
    (alt.lang.asm)
  • Re: automatic arrays versus saved, allocated arrays
    ... advantage is that the automatic array is known to be contiguous. ... For an automatic array, the compiler can interate over the entire ... the automatic array and the allocated pointer array are contiguous. ...
    (comp.lang.fortran)
  • Is the syntax for multi-dimensional arrays counter-intuitive?
    ... means that there is an array of pointers to int with a size of 5, ... of whose elements is an array of int with a size of 3. ... Allocate memory for 5 pointers to int (i.e. pointer array) ...
    (comp.lang.c)
  • Re: question on reference to array slice
    ... array by looking it up in this pointer array. ... Is this possible for Perl anyway? ... _copying_ of an array element range ...
    (comp.lang.perl.misc)
  • Re: question on reference to array slice
    ... array by looking it up in this pointer array. ... AREF => $aref, ... OFFSET => $offset, ...
    (comp.lang.perl.misc)