Main Content

Mapping an array / list without a loop - how to do it in Perl 6

Archive - Originally posted on "The Horse's Mouth" - 2016-01-03 11:17:16 - Graham Ellis

In Perl 6, you can use the >> operator to run a named piece of code (a subroutine) on each member of a list / array, returning to you a new list / array of transformed members. For example, the following code reads in all lines from a data file, splits them into individual records, and transforms each record using a sub called getGrowth.

  my @stations = slurp("rstats2015.txt").chomp.split("\n");
  my @opposite = @stations>>.&getGrowth;


Complete code - including the getGrowth sub - [here].

At Well House Consultants, we offer training in Perl 5 and - now that there's an official release out there - we're starting Perl 6 courses very soon. Please take a look at our Perl course page for more details of our plans, and in these early adaptor days, please ask if you don't see exactly what you're looking for - we can help and tailor training whether you're a newcomer to programming, a Perl 5 geek already, or (more likely for most readers) somewhere in between!