[% setvar title smart container slicing %]

This file is part of the Perl 6 Archive

Note: these documents may be out of date. Do not use as reference!

To see what is currently happening visit http://www.perl6.org/


smart container slicing


  Maintainer: David Nicol <perl6rfc@davidnicol.com>
  Date: 1 Sep 2000
  Mailing List: perl6-language-data@perl.org
  Number: 191
  Version: 1
  Status: Developing


a more concise syntax for matching all the elements in a container that are stored under keys that meet a condition


What if, instead of requiring

	@onesIwant = @hash{grep /restriction on keys/ keys %hash};

one could simply write

	@onesIwant = @hash{/restriction on keys/};

to obtain a list of all items stored in hash under keys containing the string 'restriction on keys'


When interpreting the appearance of a single regex (including an object resulting from a qr//) inside braces indicating container lookup, in a "slicing" context, the regex is applied to the keys of the container and the matching elements are returned, as per the longer syntax involving an intermediate anonymous array.


In situations where one wants to pull a list of one item out of a hash in which that item is keyed with a regex, assuming such is possible, one will need to use

	@listofoneitem = ( $hash{$regex} );

instead of the syntax appropriated by this suggestion.


With the addition to the language of regular expression syntax to deal numbers by value rather than is done now, the syntax described in this document may provide a concise way to slice multidimensional containers.