develooper Front page | perl.perl6.language | Postings from February 2004

How do I recognize an array element access?

Thread Next
From:
Austin Hastings
Date:
February 22, 2004 18:51
Subject:
How do I recognize an array element access?
Message ID:
OIEJIIJJCGOEMIJDBHAOIEGHCAAA.Austin_Hastings@Yahoo.com

Suppose I want to say:

  sub sublist(@a, $start, $cnt) {
    return @a[$start] +next --$cnt;
  }

where +next is a binary macro that takes as its lhs an array element access, and its rhs a number, and returns a list of the array element followed by the next RHS elements.

It seems obvious to me that +next has to be a macro, since it has to detect this weird condition of "accessing an array element". How do I code that?

More generally, is there any way (via trait or property or whatever) to recognize when something as basic as $_ is an array element, and get the containing array(s) and somehow do the right thing?

(I can't count the number of times I've been frustrated by map/grep because there's no way to get at the "next" or "prior" element in the sequence.)

=Austin



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