loop on a Buf/binary

$ p6 'my $fh=open "/home/linuxutil/To", :r; my Buf $f = $fh.read( 10 ); 
$fh.close; dd $f; for $f[0..*] -> $Byte { if $Byte == 0b00 {say 
"Binary"; last;}else{say $Byte}}'

Buf[uint8] $f = Buf[uint8].new(87,111,114,100,80,114,111,0,0,0)

87
111
114
100
80
114
111
Binary

To get the above to work, I had to say `$f[0..*]`. If I used
`$f`, it made one loop over the entire variable.

$ p6 'my $fh=open "/home/linuxutil/To", :r; my Buf $f = $fh.read( 10 ); 
$fh.close; dd $f; for $f -> $Byte { if $Byte == 0b00 {say "Binary"; 
last;}else{say $Byte}}'

Buf[uint8] $f = Buf[uint8].new(87,111,114,100,80,114,111,0,0,0)
Buf[uint8]:0x<57 6f 72 64 50 72 6f 00 00 00>


I never have to say `$str[0..*]` when looping over a string.  Why?

Yours in confusion,
-T
0
perl6
10/8/2018 11:05:43 AM
perl.perl6.users 1085 articles. 0 followers. Follow

9 Replies
20 Views

Similar Articles

[PageSpeed] 49

--0000000000009ea7ca0577b5b954
Content-Type: text/plain; charset="UTF-8"

On Mon, Oct 8, 2018 at 7:06 AM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> $ p6 'my $fh=open "/home/linuxutil/To", :r; my Buf $f = $fh.read( 10 );
> $fh.close; dd $f; for $f[0..*] -> $Byte { if $Byte == 0b00 {say
> "Binary"; last;}else{say $Byte}}'
>
> Buf[uint8] $f = Buf[uint8].new(87,111,114,100,80,114,111,0,0,0)
>
> 87
> 111
> 114
> 100
> 80
> 114
> 111
> Binary
>
> To get the above to work, I had to say `$f[0..*]`. If I used
> `$f`, it made one loop over the entire variable.
>
> $ p6 'my $fh=open "/home/linuxutil/To", :r; my Buf $f = $fh.read( 10 );
> $fh.close; dd $f; for $f -> $Byte { if $Byte == 0b00 {say "Binary";
> last;}else{say $Byte}}'
>
> Buf[uint8] $f = Buf[uint8].new(87,111,114,100,80,114,111,0,0,0)
> Buf[uint8]:0x<57 6f 72 64 50 72 6f 00 00 00>
>
>
> I never have to say `$str[0..*]` when looping over a string.  Why?
>

How do you loop over a string?  Doesn't 'for $str' just also run the loop
once?

Your $f is one thing (it is a scalar), so for $f will just do one thing.
You can also use for $f.list or for @$f

Curt

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

<div dir=3D"ltr"><br><br><div class=3D"gmail_quote"><div dir=3D"ltr">On Mon=
, Oct 8, 2018 at 7:06 AM ToddAndMargo via perl6-users &lt;<a href=3D"mailto=
:perl6-users@perl.org" target=3D"_blank">perl6-users@perl.org</a>&gt; wrote=
:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;bor=
der-left:1px #ccc solid;padding-left:1ex">$ p6 &#39;my $fh=3Dopen &quot;/ho=
me/linuxutil/To&quot;, :r; my Buf $f =3D $fh.read( 10 ); <br>
$fh.close; dd $f; for $f[0..*] -&gt; $Byte { if $Byte =3D=3D 0b00 {say <br>
&quot;Binary&quot;; last;}else{say $Byte}}&#39;<br>
<br>
Buf[uint8] $f =3D Buf[uint8].new(87,111,114,100,80,114,111,0,0,0)<br>
<br>
87<br>
111<br>
114<br>
100<br>
80<br>
114<br>
111<br>
Binary<br>
<br>
To get the above to work, I had to say `$f[0..*]`. If I used<br>
`$f`, it made one loop over the entire variable.<br>
<br>
$ p6 &#39;my $fh=3Dopen &quot;/home/linuxutil/To&quot;, :r; my Buf $f =3D $=
fh.read( 10 ); <br>
$fh.close; dd $f; for $f -&gt; $Byte { if $Byte =3D=3D 0b00 {say &quot;Bina=
ry&quot;; <br>
last;}else{say $Byte}}&#39;<br>
<br>
Buf[uint8] $f =3D Buf[uint8].new(87,111,114,100,80,114,111,0,0,0)<br>
Buf[uint8]:0x&lt;57 6f 72 64 50 72 6f 00 00 00&gt;<br>
<br>
<br>
I never have to say `$str[0..*]` when looping over a string.=C2=A0 Why?<br>=
</blockquote><div><br></div><div>How do you loop over a string?=C2=A0 Doesn=
&#39;t &#39;for $str&#39; just also run the loop once?</div><div><br></div>=
<div>Your $f is one thing (it is a scalar), so for $f will just do one thin=
g.</div><div>You can also use for $f.list or for @$f</div><div><br></div><d=
iv>Curt</div><div><br></div></div></div>

--0000000000009ea7ca0577b5b954--
0
curt
10/8/2018 11:14:19 AM
On 10/8/18 4:14 AM, Curt Tilmes wrote:
>=20
>=20
> On Mon, Oct 8, 2018 at 7:06 AM ToddAndMargo via perl6-users=20
> <perl6-users@perl.org <mailto:perl6-users@perl.org>> wrote:
>=20
>     $ p6 'my $fh=3Dopen "/home/linuxutil/To", :r; my Buf $f =3D $fh.rea=
d( 10 );
>     $fh.close; dd $f; for $f[0..*] -> $Byte { if $Byte =3D=3D 0b00 {say=

>     "Binary"; last;}else{say $Byte}}'
>=20
>     Buf[uint8] $f =3D Buf[uint8].new(87,111,114,100,80,114,111,0,0,0)
>=20
>     87
>     111
>     114
>     100
>     80
>     114
>     111
>     Binary
>=20
>     To get the above to work, I had to say `$f[0..*]`. If I used
>     `$f`, it made one loop over the entire variable.
>=20
>     $ p6 'my $fh=3Dopen "/home/linuxutil/To", :r; my Buf $f =3D $fh.rea=
d( 10 );
>     $fh.close; dd $f; for $f -> $Byte { if $Byte =3D=3D 0b00 {say "Bina=
ry";
>     last;}else{say $Byte}}'
>=20
>     Buf[uint8] $f =3D Buf[uint8].new(87,111,114,100,80,114,111,0,0,0)
>     Buf[uint8]:0x<57 6f 72 64 50 72 6f 00 00 00>
>=20
>=20
>     I never have to say `$str[0..*]` when looping over a string.=C2=A0 =
Why?
>=20
>=20
> How do you loop over a string?=C2=A0 Doesn't 'for $str' just also run t=
he=20
> loop once?
>=20
> Your $f is one thing (it is a scalar), so for $f will just do one thing=
=2E
> You can also use for $f.list or for @$f
>=20
> Curt
>=20

$ p6 'my $x=3D"a\nb\nc\nd"; for split( "\n", $x ) -> $Line { say $Line };=
'
a
b
c
d


--=20
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Computers are like air conditioners.
They malfunction when you open windows
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0
perl6
10/8/2018 11:20:08 AM
On 10/8/18 4:14 AM, Curt Tilmes wrote:
>=20
>=20
> On Mon, Oct 8, 2018 at 7:06 AM ToddAndMargo via perl6-users=20
> <perl6-users@perl.org <mailto:perl6-users@perl.org>> wrote:
>=20
>     $ p6 'my $fh=3Dopen "/home/linuxutil/To", :r; my Buf $f =3D $fh.rea=
d( 10 );
>     $fh.close; dd $f; for $f[0..*] -> $Byte { if $Byte =3D=3D 0b00 {say=

>     "Binary"; last;}else{say $Byte}}'
>=20
>     Buf[uint8] $f =3D Buf[uint8].new(87,111,114,100,80,114,111,0,0,0)
>=20
>     87
>     111
>     114
>     100
>     80
>     114
>     111
>     Binary
>=20
>     To get the above to work, I had to say `$f[0..*]`. If I used
>     `$f`, it made one loop over the entire variable.
>=20
>     $ p6 'my $fh=3Dopen "/home/linuxutil/To", :r; my Buf $f =3D $fh.rea=
d( 10 );
>     $fh.close; dd $f; for $f -> $Byte { if $Byte =3D=3D 0b00 {say "Bina=
ry";
>     last;}else{say $Byte}}'
>=20
>     Buf[uint8] $f =3D Buf[uint8].new(87,111,114,100,80,114,111,0,0,0)
>     Buf[uint8]:0x<57 6f 72 64 50 72 6f 00 00 00>
>=20
>=20
>     I never have to say `$str[0..*]` when looping over a string.=C2=A0 =
Why?
>=20
>=20
> How do you loop over a string?=C2=A0 Doesn't 'for $str' just also run t=
he=20
> loop once?
>=20
> Your $f is one thing (it is a scalar), so for $f will just do one thing=
=2E
> You can also use for $f.list or for @$f
>=20
> Curt
>=20


$ p6 'my $x=3D"a\nb\nc\nd"; for split( "\n", $x ) -> $Line { say $Line };=
'
a
b
c
d

--=20
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Computers are like air conditioners.
They malfunction when you open windows
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0
perl6
10/8/2018 11:20:35 AM
On 10/8/18 4:20 AM, ToddAndMargo via perl6-users wrote:
> On 10/8/18 4:14 AM, Curt Tilmes wrote:
>>
>>
>> On Mon, Oct 8, 2018 at 7:06 AM ToddAndMargo via perl6-users=20
>> <perl6-users@perl.org <mailto:perl6-users@perl.org>> wrote:
>>
>> =C2=A0=C2=A0=C2=A0 $ p6 'my $fh=3Dopen "/home/linuxutil/To", :r; my Bu=
f $f =3D $fh.read(=20
>> 10 );
>> =C2=A0=C2=A0=C2=A0 $fh.close; dd $f; for $f[0..*] -> $Byte { if $Byte =
=3D=3D 0b00 {say
>> =C2=A0=C2=A0=C2=A0 "Binary"; last;}else{say $Byte}}'
>>
>> =C2=A0=C2=A0=C2=A0 Buf[uint8] $f =3D Buf[uint8].new(87,111,114,100,80,=
114,111,0,0,0)
>>
>> =C2=A0=C2=A0=C2=A0 87
>> =C2=A0=C2=A0=C2=A0 111
>> =C2=A0=C2=A0=C2=A0 114
>> =C2=A0=C2=A0=C2=A0 100
>> =C2=A0=C2=A0=C2=A0 80
>> =C2=A0=C2=A0=C2=A0 114
>> =C2=A0=C2=A0=C2=A0 111
>> =C2=A0=C2=A0=C2=A0 Binary
>>
>> =C2=A0=C2=A0=C2=A0 To get the above to work, I had to say `$f[0..*]`. =
If I used
>> =C2=A0=C2=A0=C2=A0 `$f`, it made one loop over the entire variable.
>>
>> =C2=A0=C2=A0=C2=A0 $ p6 'my $fh=3Dopen "/home/linuxutil/To", :r; my Bu=
f $f =3D $fh.read(=20
>> 10 );
>> =C2=A0=C2=A0=C2=A0 $fh.close; dd $f; for $f -> $Byte { if $Byte =3D=3D=
 0b00 {say "Binary";
>> =C2=A0=C2=A0=C2=A0 last;}else{say $Byte}}'
>>
>> =C2=A0=C2=A0=C2=A0 Buf[uint8] $f =3D Buf[uint8].new(87,111,114,100,80,=
114,111,0,0,0)
>> =C2=A0=C2=A0=C2=A0 Buf[uint8]:0x<57 6f 72 64 50 72 6f 00 00 00>
>>
>>
>> =C2=A0=C2=A0=C2=A0 I never have to say `$str[0..*]` when looping over =
a string.=C2=A0 Why?
>>
>>
>> How do you loop over a string?=C2=A0 Doesn't 'for $str' just also run =
the=20
>> loop once?
>>
>> Your $f is one thing (it is a scalar), so for $f will just do one thin=
g.
>> You can also use for $f.list or for @$f
>>
>> Curt
>>
>=20
>=20
> $ p6 'my $x=3D"a\nb\nc\nd"; for split( "\n", $x ) -> $Line { say $Line =
};'
> a
> b
> c
> d
>=20

Or an array:

$ p6 'my @x=3D<1 b 2 c>; for @x -> $Line { say $Line };'
1
b
2
c

--=20
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Computers are like air conditioners.
They malfunction when you open windows
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0
perl6
10/8/2018 11:29:05 AM
On 10/8/18 4:29 AM, ToddAndMargo via perl6-users wrote:
> On 10/8/18 4:20 AM, ToddAndMargo via perl6-users wrote:
>> On 10/8/18 4:14 AM, Curt Tilmes wrote:
>>>
>>>
>>> On Mon, Oct 8, 2018 at 7:06 AM ToddAndMargo via perl6-users=20
>>> <perl6-users@perl.org <mailto:perl6-users@perl.org>> wrote:
>>>
>>> =C2=A0=C2=A0=C2=A0 $ p6 'my $fh=3Dopen "/home/linuxutil/To", :r; my B=
uf $f =3D $fh.read(=20
>>> 10 );
>>> =C2=A0=C2=A0=C2=A0 $fh.close; dd $f; for $f[0..*] -> $Byte { if $Byte=
 =3D=3D 0b00 {say
>>> =C2=A0=C2=A0=C2=A0 "Binary"; last;}else{say $Byte}}'
>>>
>>> =C2=A0=C2=A0=C2=A0 Buf[uint8] $f =3D Buf[uint8].new(87,111,114,100,80=
,114,111,0,0,0)
>>>
>>> =C2=A0=C2=A0=C2=A0 87
>>> =C2=A0=C2=A0=C2=A0 111
>>> =C2=A0=C2=A0=C2=A0 114
>>> =C2=A0=C2=A0=C2=A0 100
>>> =C2=A0=C2=A0=C2=A0 80
>>> =C2=A0=C2=A0=C2=A0 114
>>> =C2=A0=C2=A0=C2=A0 111
>>> =C2=A0=C2=A0=C2=A0 Binary
>>>
>>> =C2=A0=C2=A0=C2=A0 To get the above to work, I had to say `$f[0..*]`.=
 If I used
>>> =C2=A0=C2=A0=C2=A0 `$f`, it made one loop over the entire variable.
>>>
>>> =C2=A0=C2=A0=C2=A0 $ p6 'my $fh=3Dopen "/home/linuxutil/To", :r; my B=
uf $f =3D $fh.read(=20
>>> 10 );
>>> =C2=A0=C2=A0=C2=A0 $fh.close; dd $f; for $f -> $Byte { if $Byte =3D=3D=
 0b00 {say "Binary";
>>> =C2=A0=C2=A0=C2=A0 last;}else{say $Byte}}'
>>>
>>> =C2=A0=C2=A0=C2=A0 Buf[uint8] $f =3D Buf[uint8].new(87,111,114,100,80=
,114,111,0,0,0)
>>> =C2=A0=C2=A0=C2=A0 Buf[uint8]:0x<57 6f 72 64 50 72 6f 00 00 00>
>>>
>>>
>>> =C2=A0=C2=A0=C2=A0 I never have to say `$str[0..*]` when looping over=
 a string.=C2=A0 Why?
>>>
>>>
>>> How do you loop over a string?=C2=A0 Doesn't 'for $str' just also run=
 the=20
>>> loop once?
>>>
>>> Your $f is one thing (it is a scalar), so for $f will just do one thi=
ng.
>>> You can also use for $f.list or for @$f
>>>
>>> Curt
>>>
>>
>>
>> $ p6 'my $x=3D"a\nb\nc\nd"; for split( "\n", $x ) -> $Line { say $Line=
 };'
>> a
>> b
>> c
>> d
>>
>=20
> Or an array:
>=20
> $ p6 'my @x=3D<1 b 2 c>; for @x -> $Line { say $Line };'
> 1
> b
> 2
> c
>=20


Or turning $f into @f

$ p6 'my $fh=3Dopen "/home/linuxutil/To", :r; my Buf @f =3D $fh.read( 10 =
);=20
$fh.close; dd @f; for @f -> $Byte { if $Byte =3D=3D 0b00 {say "Binary";=20
last;}else{say $Byte}}'

Array[Buf] @f =3D

Array[Buf].new(Buf[uint8].new(87,111,114,100,80,114,111,0,0,0))
Buf[uint8]:0x<57 6f 72 64 50 72 6f 00 00 00>
0
perl6
10/8/2018 11:44:47 AM
On 10/8/18 4:25 AM, Curt Tilmes wrote:
>=20
>=20
> On Mon, Oct 8, 2018 at 7:21 AM ToddAndMargo via perl6-users=20
> <perl6-users@perl.org <mailto:perl6-users@perl.org>> wrote:
>=20
>      >=C2=A0 =C2=A0 =C2=A0I never have to say `$str[0..*]` when looping=
 over a string.=20
>     Why?
>      >
>      >
>      > How do you loop over a string?=C2=A0 Doesn't 'for $str' just als=
o run the
>      > loop once?
>      >
>      > Your $f is one thing (it is a scalar), so for $f will just do on=
e
>     thing.
>      > You can also use for $f.list or for @$f
>=20
>     $ p6 'my $x=3D"a\nb\nc\nd"; for split( "\n", $x ) -> $Line { say $L=
ine };'
>     a
>     b
>     c
>     d
>=20
>=20
>  =C2=A0This is not "looping over a string" -- that is actually looping =
over=20
> the list returned by split().
>=20
> You can similarly split the Buf with .list()=C2=A0 (or just use=C2=A0@$=
f).
>=20
> Curt
>=20

@$f  Worked.  Thank you!


$ p6 'my $fh=3Dopen "/home/linuxutil/To", :r; my Buf $f =3D $fh.read( 10 =
);=20
$fh.close; dd $f; for @$f -> $Byte { if $Byte =3D=3D 0b00 {say "Binary"; =

last;}else{say $Byte}}'

Buf[uint8] $f =3D Buf[uint8].new(87,111,114,100,80,114,111,0,0,0)

87
111
114
100
80
114
111
Binary
0
perl6
10/8/2018 11:50:20 AM
On 10/8/18 4:50 AM, ToddAndMargo via perl6-users wrote:
> On 10/8/18 4:25 AM, Curt Tilmes wrote:
>>
>>
>> On Mon, Oct 8, 2018 at 7:21 AM ToddAndMargo via perl6-users=20
>> <perl6-users@perl.org <mailto:perl6-users@perl.org>> wrote:
>>
>> =C2=A0=C2=A0=C2=A0=C2=A0 >=C2=A0 =C2=A0 =C2=A0I never have to say `$st=
r[0..*]` when looping over a=20
>> string. =C2=A0=C2=A0=C2=A0 Why?
>> =C2=A0=C2=A0=C2=A0=C2=A0 >
>> =C2=A0=C2=A0=C2=A0=C2=A0 >
>> =C2=A0=C2=A0=C2=A0=C2=A0 > How do you loop over a string?=C2=A0 Doesn'=
t 'for $str' just also=20
>> run the
>> =C2=A0=C2=A0=C2=A0=C2=A0 > loop once?
>> =C2=A0=C2=A0=C2=A0=C2=A0 >
>> =C2=A0=C2=A0=C2=A0=C2=A0 > Your $f is one thing (it is a scalar), so f=
or $f will just do one
>> =C2=A0=C2=A0=C2=A0 thing.
>> =C2=A0=C2=A0=C2=A0=C2=A0 > You can also use for $f.list or for @$f
>>
>> =C2=A0=C2=A0=C2=A0 $ p6 'my $x=3D"a\nb\nc\nd"; for split( "\n", $x ) -=
> $Line { say=20
>> $Line };'
>> =C2=A0=C2=A0=C2=A0 a
>> =C2=A0=C2=A0=C2=A0 b
>> =C2=A0=C2=A0=C2=A0 c
>> =C2=A0=C2=A0=C2=A0 d
>>
>>
>> =C2=A0=C2=A0This is not "looping over a string" -- that is actually lo=
oping over=20
>> the list returned by split().
>>
>> You can similarly split the Buf with .list()=C2=A0 (or just use=C2=A0@=
$f).
>>
>> Curt
>>
>=20
> @$f=C2=A0 Worked.=C2=A0 Thank you!
>=20
>=20
> $ p6 'my $fh=3Dopen "/home/linuxutil/To", :r; my Buf $f =3D $fh.read( 1=
0 );=20
> $fh.close; dd $f; for @$f -> $Byte { if $Byte =3D=3D 0b00 {say "Binary"=
;=20
> last;}else{say $Byte}}'
>=20
> Buf[uint8] $f =3D Buf[uint8].new(87,111,114,100,80,114,111,0,0,0)
>=20
> 87
> 111
> 114
> 100
> 80
> 114
> 111
> Binary

I take it that `Buf` is a special type of array that the normal
rules do not apply to.
0
perl6
10/8/2018 11:52:43 AM
--000000000000ea7a140577b6d433
Content-Type: text/plain; charset="UTF-8"

On Mon, Oct 8, 2018 at 7:53 AM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> I take it that `Buf` is a special type of array that the normal
> rules do not apply to.
>

I would say rather than each of them (Buf and Array) are special types of
Positional -- they 'do' the Positional role -- That role implies certain
behaviors that they both implement (among many other types/roles).  That
includes support for [] indexing and for @$var looping.  They each also
have special behaviors beyond the Positional stuff.

Curt

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

<div dir=3D"ltr"><br><br><div class=3D"gmail_quote"><div dir=3D"ltr">On Mon=
, Oct 8, 2018 at 7:53 AM ToddAndMargo via perl6-users &lt;<a href=3D"mailto=
:perl6-users@perl.org">perl6-users@perl.org</a>&gt; wrote:</div><blockquote=
 class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc soli=
d;padding-left:1ex">
I take it that `Buf` is a special type of array that the normal<br>
rules do not apply to.<br></blockquote><div><br></div><div>I would say rath=
er than each of them (Buf and Array) are special types of Positional -- the=
y &#39;do&#39; the Positional role -- That role implies certain behaviors t=
hat they both implement (among many other types/roles).=C2=A0 That includes=
 support for [] indexing and for=C2=A0@$var looping.=C2=A0 They each also h=
ave special behaviors beyond the Positional stuff.</div><div><br></div><div=
>Curt</div><div>=C2=A0</div></div></div>

--000000000000ea7a140577b6d433--
0
curt
10/8/2018 12:33:32 PM
On 10/8/18 5:33 AM, Curt Tilmes wrote:
>=20
>=20
> On Mon, Oct 8, 2018 at 7:53 AM ToddAndMargo via perl6-users=20
> <perl6-users@perl.org <mailto:perl6-users@perl.org>> wrote:
>=20
>     I take it that `Buf` is a special type of array that the normal
>     rules do not apply to.
>=20
>=20
> I would say rather than each of them (Buf and Array) are special types =

> of Positional -- they 'do' the Positional role -- That role implies=20
> certain behaviors that they both implement (among many other=20
> types/roles).=C2=A0 That includes support for [] indexing and for=C2=A0=
@$var=20
> looping.=C2=A0 They each also have special behaviors beyond the Positio=
nal stuff.
>=20
> Curt

Hi Curt,

Makes sense, thank you!

I added @$var to my notes, so I won't miss it again.

-T
0
perl6
10/9/2018 8:18:16 AM
Reply: