in reply to Can you take a slice of an array reference?

How about:
@a=qw(one two three four five six); $r=\@a; print @{$r}[0..2]; # Gives onetwothree

Replies are listed 'Best First'.
Re: Re: Can you take a slice of an array reference?
by Anonymous Monk on May 24, 2001 at 13:32 UTC
      Because when I'm explaining in class, or in a book, I usually leave the braces around the expression for clarity. Given:
      $a=\@foo;
      that $a is now a reference to an array and, syntatically, it can be used as a stand-in for an array name. Now knowing this, treat @{$a} as you would @foo and get the same results:
      @{$a}; # Same as @foo ${$a}[1]; # Same as $foo[1] @{$a}[0..2]; # Same as @foo[0..2]
      It's just a teaching tool, and I've used it before with great success. And when they're confortable with this you can remove the braces (reminding them of precedence) and when they've got that down pat, show them -> where they get a dereference for free.