Re: question on reference to array slice



On Sun, 29 Jul 2007 18:04:28 -0000 Rick <rick.peng.du@xxxxxxxxx> wrote:

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

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

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

R> Is this possible for Perl anyway?

It depends on your purpose. If you aim for speed, just use C (Inline::C
for example). Otherwise, to make your own and everyone else's life
easier, use a function to translate the offsets or a constant. Even
better, explain what you're trying to do; it may be that hashes or
multidimensional arrays or array slices are the right answer for you.

Note you don't have to call the offset function every time, only at the
beginning to find out the offset. So it's not a big speed penalty.

Of course, you can always write your own Tie module, but those are IMHO
the last resort.

Ted

.



Relevant Pages

  • Re: char **argv & char *argv[]
    ... "pointer to pointer to char". ... >> pointer)) pointing to the first element of an array. ... so we have to start adding more context. ... type "pointer to char", rather than "array MISSING_SIZE of char". ...
    (comp.lang.c)
  • Re: multi dimensional arrays as one dimension array
    ... please - where does the standard say that such a conversion ... Pointer conversion yields a pointer to the same object as ... exist only where there are array declarations. ...
    (comp.lang.c)
  • Re: Evaluating unary *
    ... 'arr' exists, ... value can be used with the same syntax as would be used to access a 2D array of the kind you're referring to, but that 2D array is just a different way of looking as the same object that was already created by the definition of 'arr'. ... to me, it makes sense to return a pointer to the first value of an array, but to return the address of the pointer to the first value of an array, is not directly possible as such. ... lea eax, ...
    (comp.std.c)
  • Re: "Mastering C Pointers"....
    ... A pointer is a kind of variable that can "point to" some object. ... has a type (pointer to int), and a value of some kind. ... You may know that you can access these integers by using array notation ... The function will take one argument, a string, and will return the length ...
    (comp.lang.c)
  • Re: Copying an array slice (Was: Re: Difficulties with passing multi-dimensional arrays)
    ... > the pointer to array of unknown size. ... but it still compiles without a cast. ... unknown at compile time so nothing can be checked. ...
    (comp.lang.c)