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 <perl6-us...@perl.org <mailto:perl6-us...@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>

Reply via email to