On 12/30/20 5:39 PM, ToddAndMargo via perl6-users wrote:
Hi All,

In the following for loop:

     for ^$nCount -> $i {

What is the ^ doing?

Confused again,
-T

With wonderful explanations for many others, my notes:


^ note: ^3 means the integer "just before" 3 (zero is presume to be the start point)

         3^ means the integer "just after" 3  (an ending point is required)


Looping using an integer (avoids having to use a C loop):

   # loop to "just before" $x starting at 0 by 1
   > my $x=3; for ^$x -> $i { print "i = $i\n"; }
   i = 0
   i = 1
   i = 2

   # loop from 3 to 5 by 1
   > for 3..5 -> $i { print "i = $i\n"; }
   i = 3
   i = 4
   i = 5

   # loop from 3 to "just before" 6 by 1
   > for 3..^6 -> $i { print "i = $i\n"; }
   i = 3
   i = 4
   i = 5

   # loop from "just after" 3 to 6 by 1
   > for 3^..6 -> $i { print "i = $i\n"; }
   i = 4
   i = 5
   i = 6

   # loop from "just after" 3 to "just before" 7 by 1
   > for 3^..^7 -> $i { print "i = $i\n"; }
   i = 4
   i = 5
   i = 6


Reply via email to