On 10/8/18 1:34 AM, Peter Pentchev wrote:
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".
Been there, done that already. No idea what it said.
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
Hi Peter,
Perfect! Exactly what I was after!
Thank you!
$ p6 'my $fh=open "/home/linuxutil/To", :r; my Buf $f = $fh.read( 10 );
$fh.close; say $f[1..3]; say $f;'
(111 114 100)
Buf[uint8]:0x<57 6f 72 64 50 72 6f 00 00 00>
-T