On Mon, Oct 08, 2018 at 01:38:54AM -0700, ToddAndMargo via perl6-users wrote: > 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.
OK, so for future reference, when you see a reference page for a type like that, look at the left side to see which roles it implements and which methods it takes from these roles. As I wrote below, in this case the Positional role could have been a clue that you can address a Buf object using [index] (I seem to remember another thread of yours with people explaining the Positional role). Also, in the future, take a look at the code in the examples; in this particular case, the use of "$b[1]" could have been a hint. > > 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! Glad it worked out for you! 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
signature.asc
Description: PGP signature