On Mon, Oct 08, 2018 at 01:25:31AM -0700, ToddAndMargo via perl6-users wrote:
> Hi All!
> 
> Question: I am using `read` to read the first 400 bytes of an unknown file
> (could be a binary file).  The 400 bytes go into a variable
> of type "Buf".  This is not a string.
> 
> p6 'my $fh=open "/home/linuxutil/To", :r; my Buf $f = $fh.read( 400 );
> $fh.close;'
> 
> Now in $f, I want to look at each byte one at a time for a
> bitwise pattern using bitwise AND.
> 
> How do I address each byte?
> 
> `dd` seems to get me the information I need, but it prints it:
> 
>     $ p6 'my $fh=open "/home/linuxutil/To", :r; my Buf $f = $fh.read( 10 );
> $fh.close; dd $f;'
> 
>     Buf[uint8] $f = Buf[uint8].new(87,111,114,100,80,114,111,0,0,0)
> 
> An array of bytes would be great.

Point a browser at https://docs.perl6.org/ and click on "Types" in
the top ribbon.  You will see a list of all the Perl 6 built-in types;
"Buf" is there near the top.  Click on "Buf".

Now there are two clues as to what you want: one of them is that
the table of contents on the left has a section "Routines supplied by
role Positional", and the other one is that the very first example
has a line saying "$b[1] = 42".

So you can use a Buf object as an array of whatever it contains.

G'luck,
Peter

-- 
Peter Pentchev  roam@{ringlet.net,debian.org,FreeBSD.org} p...@storpool.com
PGP key:        http://people.FreeBSD.org/~roam/roam.key.asc
Key fingerprint 2EE7 A7A5 17FC 124C F115  C354 651E EFB0 2527 DF13

Attachment: signature.asc
Description: PGP signature

Reply via email to