Is there a backward "for @"

Hi All,

This seems like a trivial question, but I really adore
the "for" loops.  Is there a way to do the backwards?
In other words, start at the end of the array and loop
to the beginning?  Does the "next" and "last" work in
this?

Inquiring minds want to know!

Many thanks,
-T
0
ToddAndMargo
5/15/2018 10:31:07 PM
perl.perl6.users 954 articles. 0 followers. Follow

6 Replies
11 Views

Similar Articles

[PageSpeed] 9

On 05/15/2018 03:31 PM, ToddAndMargo wrote:
> do the backwards

do theM backwards

mumble, mumble
0
ToddAndMargo
5/15/2018 10:34:50 PM
On Tue, May 15, 2018 at 03:31:07PM -0700, ToddAndMargo wrote:
: Hi All,
: 
: This seems like a trivial question, but I really adore
: the "for" loops.  Is there a way to do the backwards?
: In other words, start at the end of the array and loop
: to the beginning?  Does the "next" and "last" work in
: this?

Just use the reverse method:

    > my @foo = <a b c>;
    [a b c]
    > for @foo.reverse { .say }
    c
    b
    a

or (as in Perl 5) the reverse function:

    > for reverse @foo { .say }
    c
    b
    a

and yes, "next" and "last" work for those loops too, since they are
controlled by the "for", not by the expression you feed to the "for".

Larry
0
larry
5/15/2018 10:49:01 PM
On 05/15/2018 03:49 PM, Larry Wall wrote:
> On Tue, May 15, 2018 at 03:31:07PM -0700, ToddAndMargo wrote:
> : Hi All,
> :
> : This seems like a trivial question, but I really adore
> : the "for" loops.  Is there a way to do the backwards?
> : In other words, start at the end of the array and loop
> : to the beginning?  Does the "next" and "last" work in
> : this?
> 
> Just use the reverse method:
> 
>      > my @foo = <a b c>;
>      [a b c]
>      > for @foo.reverse { .say }
>      c
>      b
>      a
> 
> or (as in Perl 5) the reverse function:
> 
>      > for reverse @foo { .say }
>      c
>      b
>      a
> 
> and yes, "next" and "last" work for those loops too, since they are
> controlled by the "for", not by the expression you feed to the "for".
> 
> Larry
> 

Hi Larry,

Awesome.  I just copied your response down into my "loops"
keeper file.

I use loops and loops with split "a lot".

Thank you!

-T

-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Computers are like air conditioners.
They malfunction when you open windows
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0
ToddAndMargo
5/15/2018 11:34:53 PM
On 05/15/2018 04:34 PM, ToddAndMargo wrote:
> On 05/15/2018 03:49 PM, Larry Wall wrote:
>> On Tue, May 15, 2018 at 03:31:07PM -0700, ToddAndMargo wrote:
>> : Hi All,
>> :
>> : This seems like a trivial question, but I really adore
>> : the "for" loops.=C2=A0 Is there a way to do the backwards?
>> : In other words, start at the end of the array and loop
>> : to the beginning?=C2=A0 Does the "next" and "last" work in
>> : this?
>>
>> Just use the reverse method:
>>
>> =C2=A0=C2=A0=C2=A0=C2=A0 > my @foo =3D <a b c>;
>> =C2=A0=C2=A0=C2=A0=C2=A0 [a b c]
>> =C2=A0=C2=A0=C2=A0=C2=A0 > for @foo.reverse { .say }
>> =C2=A0=C2=A0=C2=A0=C2=A0 c
>> =C2=A0=C2=A0=C2=A0=C2=A0 b
>> =C2=A0=C2=A0=C2=A0=C2=A0 a
>>
>> or (as in Perl 5) the reverse function:
>>
>> =C2=A0=C2=A0=C2=A0=C2=A0 > for reverse @foo { .say }
>> =C2=A0=C2=A0=C2=A0=C2=A0 c
>> =C2=A0=C2=A0=C2=A0=C2=A0 b
>> =C2=A0=C2=A0=C2=A0=C2=A0 a
>>
>> and yes, "next" and "last" work for those loops too, since they are
>> controlled by the "for", not by the expression you feed to the "for".
>>
>> Larry
>>
>=20
> Hi Larry,
>=20
> Awesome.=C2=A0 I just copied your response down into my "loops"
> keeper file.
>=20
> I use loops and loops with split "a lot".
>=20
> Thank you!
>=20
> -T
>=20

It is annoying when I read something back from Linux
and they use a <nil> at the start and stop of a
string (the Secondary Clipboard for instance).

But I just use `if not $line {next}` to jump over it.

-T

--=20
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Computers are like air conditioners.
They malfunction when you open windows
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0
ToddAndMargo
5/15/2018 11:39:33 PM
--000000000000f585d5056c47dad0
Content-Type: text/plain; charset="UTF-8"

Slightly more idiomatic might be `next unless $line`.

On Tue, May 15, 2018 at 7:39 PM ToddAndMargo <ToddAndMargo@zoho.com> wrote:

> On 05/15/2018 04:34 PM, ToddAndMargo wrote:
> > On 05/15/2018 03:49 PM, Larry Wall wrote:
> >> On Tue, May 15, 2018 at 03:31:07PM -0700, ToddAndMargo wrote:
> >> : Hi All,
> >> :
> >> : This seems like a trivial question, but I really adore
> >> : the "for" loops.  Is there a way to do the backwards?
> >> : In other words, start at the end of the array and loop
> >> : to the beginning?  Does the "next" and "last" work in
> >> : this?
> >>
> >> Just use the reverse method:
> >>
> >>      > my @foo = <a b c>;
> >>      [a b c]
> >>      > for @foo.reverse { .say }
> >>      c
> >>      b
> >>      a
> >>
> >> or (as in Perl 5) the reverse function:
> >>
> >>      > for reverse @foo { .say }
> >>      c
> >>      b
> >>      a
> >>
> >> and yes, "next" and "last" work for those loops too, since they are
> >> controlled by the "for", not by the expression you feed to the "for".
> >>
> >> Larry
> >>
> >
> > Hi Larry,
> >
> > Awesome.  I just copied your response down into my "loops"
> > keeper file.
> >
> > I use loops and loops with split "a lot".
> >
> > Thank you!
> >
> > -T
> >
>
> It is annoying when I read something back from Linux
> and they use a <nil> at the start and stop of a
> string (the Secondary Clipboard for instance).
>
> But I just use `if not $line {next}` to jump over it.
>
> -T
>
> --
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Computers are like air conditioners.
> They malfunction when you open windows
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>

--000000000000f585d5056c47dad0
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Slightly more idiomatic might be `next unless $line`.<br><=
/div><br><div class=3D"gmail_quote"><div dir=3D"ltr">On Tue, May 15, 2018 a=
t 7:39 PM ToddAndMargo &lt;<a href=3D"mailto:ToddAndMargo@zoho.com">ToddAnd=
Margo@zoho.com</a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote" st=
yle=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 05=
/15/2018 04:34 PM, ToddAndMargo wrote:<br>
&gt; On 05/15/2018 03:49 PM, Larry Wall wrote:<br>
&gt;&gt; On Tue, May 15, 2018 at 03:31:07PM -0700, ToddAndMargo wrote:<br>
&gt;&gt; : Hi All,<br>
&gt;&gt; :<br>
&gt;&gt; : This seems like a trivial question, but I really adore<br>
&gt;&gt; : the &quot;for&quot; loops.=C2=A0 Is there a way to do the backwa=
rds?<br>
&gt;&gt; : In other words, start at the end of the array and loop<br>
&gt;&gt; : to the beginning?=C2=A0 Does the &quot;next&quot; and &quot;last=
&quot; work in<br>
&gt;&gt; : this?<br>
&gt;&gt;<br>
&gt;&gt; Just use the reverse method:<br>
&gt;&gt;<br>
&gt;&gt; =C2=A0=C2=A0=C2=A0=C2=A0 &gt; my @foo =3D &lt;a b c&gt;;<br>
&gt;&gt; =C2=A0=C2=A0=C2=A0=C2=A0 [a b c]<br>
&gt;&gt; =C2=A0=C2=A0=C2=A0=C2=A0 &gt; for @foo.reverse { .say }<br>
&gt;&gt; =C2=A0=C2=A0=C2=A0=C2=A0 c<br>
&gt;&gt; =C2=A0=C2=A0=C2=A0=C2=A0 b<br>
&gt;&gt; =C2=A0=C2=A0=C2=A0=C2=A0 a<br>
&gt;&gt;<br>
&gt;&gt; or (as in Perl 5) the reverse function:<br>
&gt;&gt;<br>
&gt;&gt; =C2=A0=C2=A0=C2=A0=C2=A0 &gt; for reverse @foo { .say }<br>
&gt;&gt; =C2=A0=C2=A0=C2=A0=C2=A0 c<br>
&gt;&gt; =C2=A0=C2=A0=C2=A0=C2=A0 b<br>
&gt;&gt; =C2=A0=C2=A0=C2=A0=C2=A0 a<br>
&gt;&gt;<br>
&gt;&gt; and yes, &quot;next&quot; and &quot;last&quot; work for those loop=
s too, since they are<br>
&gt;&gt; controlled by the &quot;for&quot;, not by the expression you feed =
to the &quot;for&quot;.<br>
&gt;&gt;<br>
&gt;&gt; Larry<br>
&gt;&gt;<br>
&gt; <br>
&gt; Hi Larry,<br>
&gt; <br>
&gt; Awesome.=C2=A0 I just copied your response down into my &quot;loops&qu=
ot;<br>
&gt; keeper file.<br>
&gt; <br>
&gt; I use loops and loops with split &quot;a lot&quot;.<br>
&gt; <br>
&gt; Thank you!<br>
&gt; <br>
&gt; -T<br>
&gt; <br>
<br>
It is annoying when I read something back from Linux<br>
and they use a &lt;nil&gt; at the start and stop of a<br>
string (the Secondary Clipboard for instance).<br>
<br>
But I just use `if not $line {next}` to jump over it.<br>
<br>
-T<br>
<br>
-- <br>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>
Computers are like air conditioners.<br>
They malfunction when you open windows<br>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>
</blockquote></div>

--000000000000f585d5056c47dad0--
0
awwaiid
5/16/2018 12:32:23 AM
On 05/15/2018 05:32 PM, Brock Wilcox wrote:
> Slightly more idiomatic might be `next unless $line`.

Interesting!  Thank you!
0
ToddAndMargo
5/16/2018 12:42:01 AM
Reply: