Re: loop on a Buf/binary
On 10/8/18 5:33 AM, Curt Tilmes wrote: On Mon, Oct 8, 2018 at 7:53 AM ToddAndMargo via perl6-users mailto: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 Hi Curt, Makes sense, thank you! I added @$var to my notes, so I won't miss it again. -T
Re: loop on a Buf/binary
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
Re: loop on a Buf/binary
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 mailto:perl6-users@perl.org>> wrote: > 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 $ p6 'my $x="a\nb\nc\nd"; for split( "\n", $x ) -> $Line { say $Line };' a b c d This is not "looping over a string" -- that is actually looping over the list returned by split(). You can similarly split the Buf with .list() (or just use @$f). Curt @$f Worked. Thank you! $ 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) 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.
Re: loop on a Buf/binary
On 10/8/18 4:25 AM, Curt Tilmes wrote: On Mon, Oct 8, 2018 at 7:21 AM ToddAndMargo via perl6-users mailto:perl6-users@perl.org>> wrote: > 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 $ p6 'my $x="a\nb\nc\nd"; for split( "\n", $x ) -> $Line { say $Line };' a b c d This is not "looping over a string" -- that is actually looping over the list returned by split(). You can similarly split the Buf with .list() (or just use @$f). Curt @$f Worked. Thank you! $ 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) 87 111 114 100 80 114 111 Binary
Re: loop on a Buf/binary
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 mailto: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 $ p6 'my $x="a\nb\nc\nd"; for split( "\n", $x ) -> $Line { say $Line };' a b c d Or an array: $ p6 'my @x=<1 b 2 c>; for @x -> $Line { say $Line };' 1 b 2 c Or turning $f into @f $ 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}}' Array[Buf] @f = 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>
Re: loop on a Buf/binary
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 mailto: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 $ p6 'my $x="a\nb\nc\nd"; for split( "\n", $x ) -> $Line { say $Line };' a b c d Or an array: $ p6 'my @x=<1 b 2 c>; for @x -> $Line { say $Line };' 1 b 2 c -- ~~ Computers are like air conditioners. They malfunction when you open windows ~~
Re: loop on a Buf/binary
On 10/8/18 4:14 AM, Curt Tilmes wrote: On Mon, Oct 8, 2018 at 7:06 AM ToddAndMargo via perl6-users mailto: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 $ p6 'my $x="a\nb\nc\nd"; for split( "\n", $x ) -> $Line { say $Line };' a b c d -- ~~ Computers are like air conditioners. They malfunction when you open windows ~~
Re: loop on a Buf/binary
On 10/8/18 4:14 AM, Curt Tilmes wrote: On Mon, Oct 8, 2018 at 7:06 AM ToddAndMargo via perl6-users mailto: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 $ p6 'my $x="a\nb\nc\nd"; for split( "\n", $x ) -> $Line { say $Line };' a b c d -- ~~ Computers are like air conditioners. They malfunction when you open windows ~~
Re: loop on a Buf/binary
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
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