develooper Front page | perl.perl6.language | Postings from May 2001

Re: Slice refs

Thread Previous | Thread Next
From:
Damian Conway
Date:
May 20, 2001 20:31
Subject:
Re: Slice refs
Message ID:
200105210331.NAA47641@indy05.csse.monash.edu.au

Peter Scott wrote:

   >
   > Um, this is a tiny little diversion here prompted by something that
   > came up on perl-beginners, of all places... it's not possible in
   > perl 5 to make a reference to an array or hash slice without doing
   > some copying.
   >

Hey, this is *Perl*! Of course it's possible...

-----------cut-----------cut-----------cut-----------cut-----------cut----------

@A = (1..10);	# array
@I = (3..5);	# indices of desired slice

$ref =
 sub{my%k;@k{@{+pop}}=\(@_);splice@_,$_,!$k{$_}for reverse 0..@_;\@_}->(@A,\@I);

print "@A\n";
print "@A[@I]\n";
print "@$ref\n";

$ref->[1] = 99;

print "@A\n";
print "@A[@I]\n";
print "@$ref\n";
   
-----------cut-----------cut-----------cut-----------cut-----------cut----------

;-)


   >
   > It would be nice if perl 6 made that possible.
   >

I guess that would be:

  $ref = sub{my%k;%k{+pop}=\(@_);splice@_,$_,!%k{$_}for @_..0:-1;@_}.(@A,\@I);


   >
   > Maybe it already does and I haven't grokked that from the exegeses yet.
   >

I'm pretty sure Exegesis II doesn't mention *that* particular technique ;-)


Damian


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About