> On 28 Feb 2017, at 22:20, ToddAndMargo <toddandma...@zoho.com> wrote:
> 
> Hi All,
> 
> There are times when I want to know th4e index of an array
> when I am in a "for @array" loop.  I can do it with a
> variable outside the for loop and increment it, but
> I would line to know know if there is a way to incorporate
> it in the loop command.
> 
> This is my long winded way of doing it in Perl 5:
> 
> while (my ($Index, $Element) = each ( @Sorted_List ) ) { do something }

use .kv:

$ 6 'my @a = <a b c>; for @a.kv -> $index,$value { dd $index, $value }'
Int $index = 0
Str $value = "a"
Int $index = 1
Str $value = "b"
Int $index = 2
Str $value = “c"

Mind you, you can actually chain that with your sort:

$ 6 'my @a = <c b a>; for @a.sort.kv -> $index,$value { dd $index, $value }'
Int $index = 0
Str $value = "a"
Int $index = 1
Str $value = "b"
Int $index = 2
Str $value = “c"

HTH,

Liz

Reply via email to