Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
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

Replies are listed 'Best First'.
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?
[Lady_Aleena]: Corion, I knew that, I was just giving Discipulus a hard time.
choroba likes the story at the end of the node
[choroba]: reminds me of the days at the bank
[Corion]: Lady_Aleena: Hehe ;-D
[Lady_Aleena]: Corion, it was PerlMonks who kept pushing me to get Linux, so now I know some of the commands to use in word play.
[Corion]: Lady_Aleena: :-D
[Lady_Aleena]: Corion, so do you think the gods would be open to taking over Cookies should I disappear for too long a time?

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2017-04-26 08:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (471 votes). Check out past polls.