reference to a partial array 
Author Message
 reference to a partial array

Dear all,

Can we get a reference to a partial array?

I express my question conceptually in C++

void main() {
    readonly_recursive("abcdefg");

Quote:
}

void readonly_recursive(char *reference) {
    if(reference && *reference) {
        char head,*tail;
        head= *reference;  tail=reference+1;
        ...
        readonly_recursive(tail);
        ...
    }

Quote:
}

In Perl the code would look like:

readonly_recursive(['a'..'g']);

sub readonly_recursive {


    my ($head,$tail);

    ...
    readonly_recursive($tail);
    ...

Quote:
}

Of course, the code there


is not what I want (reference to a partial array)
because it will do a copy.

It would cause a slow down if I want to process
a very large array just in a read only manner.

Can we do that in Perl (just reference, no copy)?

Thank you very much.

John Lin



Wed, 18 Jun 1902 08:00:00 GMT  
 reference to a partial array
[posted & mailed]

Quote:

> Can we get a reference to a partial array?

No.  You can get a reference to one scalar element of an array, but that
won't help you.

[snip explanation]

Quote:
> It would cause a slow down if I want to process
> a very large array just in a read only manner.

Since the array is read only, you should not need any array ops.  Pass a
reference to the array and an index to the first element.

sub readonly_recursive {

    ...
    readonly_recursive($arref, $i + 1);
    ...

Quote:
}

--
Rick Delaney



Wed, 18 Jun 1902 08:00:00 GMT  
 reference to a partial array


Wed, 18 Jun 1902 08:00:00 GMT  
 reference to a partial array

Quote:
>> Can we get a reference to a partial array?
>No.  You can get a reference to one scalar element of an array,
>but that won't help you.

Hey, I've got an idea.  Since the following code works

I think it won't be too difficult for Perl to do


I hope Perl would consider enabling this in the next version.

Hey, what about dangling reference problem?

Currently the following code works,

$x=5;
$reference= \$x;
undef $x;
print $$reference;

so I think it won't be too difficult.

What do you think about it?  Do you hope to have
magic Lvalue in a reference variable?
It could become a very useful feature in Perl.

Thank you for your advice.

John Lin



Wed, 18 Jun 1902 08:00:00 GMT  
 reference to a partial array

Quote:

>>> Can we get a reference to a partial array?

>>No.  You can get a reference to one scalar element of an array,
>>but that won't help you.

>Hey, I've got an idea.  Since the following code works





Quote:
>I think it won't be too difficult for Perl to do



Sorry, currently available.    In list context, the rhs is a list of
four references.   But here we have a scalar context, and we get the
fourth element of the list.

Quote:
>Hey, what about dangling reference problem?

>Currently the following code works,

>$x=5;
>$reference= \$x;
>undef $x;
>print $$reference;

What dangling reference?    undef $x doesn't cause the variable to
disappear, just its value.    And in any case, in Perl reference counting
prevents dangling references.

To find out what's happening in examples like this, it's a good idea to
try them out in the Perl de{*filter*}, and print out the various references
so you can see which variable is which:



0  SCALAR(0x182358)
   -> 8
1  SCALAR(0x182364)
   -> 9
2  SCALAR(0x182370)
   -> 10
3  SCALAR(0x18237c)
   -> 11

0  SCALAR(0x18237c)
   -> 11
  DB<4> $x = 5

  DB<5> x \$x
0  SCALAR(0x185ca4)
   -> 5
  DB<6> undef $x

  DB<7> x \$x
0  SCALAR(0x185ca4)          <-----------  note same reference as last time
   -> undef
  DB<8>

Mike Guy



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. clean up an array of references from references which are pointing into empty arrays

2. ?: Array Reference vs Anon Array Reference

3. Convert reference to array into an array of references

4. filehandles in reference array within reference hash

5. Function parameter of Array and Array reference.

6. dereference entire array of array references

7. references (one element of an array to an array)

8. Sorting an array of arrays of references

9. array of array references

10. Referencing data in an array of arrays

11. array of array references

12. partial buffered writes

 

 
Powered by phpBB® Forum Software