Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: reference to an array slice?

by broquaint (Abbot)
on Sep 26, 2003 at 10:25 UTC ( #294371=note: print w/ replies, xml ) Need Help??


in reply to reference to an array slice?

Contrary to the above nodes this can be done, but perhaps not as exactly as you had envisaged

{ package Tie::Array::Slice; use Tie::Array; @ISA = 'Tie::StdArray'; use Data::Dumper; use strict; sub FETCH { ${ $_[0]->[ $_[1] ] } } sub STORE { ref $_[2] ? $_[0]->[ $_[1] ] = $_[2] : ${ $_[0]->[ $_[1] ] } = $_[2]; } } use strict; my @a = qw/ one two three four /; tie my @b, 'Tie::Array::Slice'; @b = (\(@a))[1 .. 2]; print "\$b[1]: $b[1]\n"; print "\$a[2]: $a[2]\n"; print "before \@a: @a\n"; $b[0] = 'xxx'; print "after \@a: @a\n"; __output__ $b[1]: three $a[2]: three before @a: one two three four after @a: one xxx three four
Or if you're in feeling devious then TheDamian illustrates how alias an array slice in this perl6-language post in perl5 :>
HTH

_________
broquaint


Comment on Re: reference to an array slice?
Download Code
Re: Re: reference to an array slice?
by BrowserUk (Pope) on Sep 26, 2003 at 11:37 UTC

    It's a tad more efficient to use perl's built-in aliasing mechanism instead of a tie, at the slight cost of using an array reference rather than an array direct.

    P:\test>test3 1 2 3 4 It changed 6 So did this 8 9 10 Rate tie sub tie 473/s -- -96% sub 11689/s 2370% -- 0 0 0 -15402 -15402 -15402 -15402 -15402 0 0

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
    If I understand your problem, I can solve it! Of course, the same can be said for you.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://294371]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (9)
As of 2014-07-10 18:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (215 votes), past polls