develooper Front page | perl.perl6.users | Postings from February 2019

Re: binary test and position?

Thread Previous | Thread Next
From:
ToddAndMargo via perl6-users
Date:
February 3, 2019 03:06
Subject:
Re: binary test and position?
Message ID:
a9851444-cd33-7ffd-cf26-151abeebae99@zoho.com
 > On Fri, Feb 1, 2019 at 11:02 PM ToddAndMargo via perl6-users> 
<perl6-users@perl.org> wrote:
 >>
 >> On 2/1/19 8:26 PM, ToddAndMargo via perl6-users wrote:
 >>> On 2/1/19 8:07 PM, ToddAndMargo via perl6-users wrote:
 >>>> On 2/1/19 8:03 PM, ToddAndMargo via perl6-users wrote:
 >>>>>   > On Fri, Feb 1, 2019 at 9:37 PM ToddAndMargo via perl6-users
 >>>>>   > <perl6-users@perl.org> wrote:
 >>>>>   >>
 >>>>>   >> On 2/1/19 7:22 PM, ToddAndMargo via perl6-users wrote:
 >>>>>   >>> Hi All,
 >>>>>   >>>
 >>>>>   >>> On a type Buf, what do I use to check for the
 >>>>>   >>> position of a byte pattern?
 >>>>>   >>>
 >>>>>   >>>
 >>>>>   >>> Many thanks,
 >>>>>   >>> -T
 >>>>>   >>
 >>>>>   >>
 >>>>>   >> Basically, what am I doing wrong here?
 >>>>>   >>
 >>>>>   >> $ p6 'my $handle=open("filever.exe", :bin, :ro); my Buf $b; $b=
 >>>>>   >> $handle.read(5); say $b; say $b[2..4];; if ( $b[2..4] eq
 >>>>> 0x90,0x00,0x04
 >>>>>   >> ) {say "y";} else {say "n"}; $handle.close;'
 >>>>>   >> Buf[uint8]:0x<4D 5A 90 00 03>
 >>>>>   >> (144 0 3)
 >>>>>   >> y
 >>>>>   >>
 >>>>>   >>
 >>>>>   >> I am testing to see if the pattern 0x90 0x00 0x04 exists,
 >>>>>   >> which is does not.
 >>>>>
 >>>>>
 >>>>> On 2/1/19 7:57 PM, Brad Gilbert wrote:
 >>>>>> `eq` is string equality
 >>>>>> `==` is numeric equality
 >>>>>>
 >>>>>> a Buf is neither.
 >>>>>>
 >>>>>> You want `eqv` (equivalent)
 >>>>>>
 >>>>>>       $b[2..4] eqv (0x90,0x00,0x04)
 >>>>>>
 >>>>>
 >>>>> That was it.  Thank you!
 >>>>>
 >>>>> $ p6 'my $handle=open("filever.exe", :bin, :ro); my Buf $b; $b=
 >>>>> $handle.read(5); say $b; if ( $b[2..4] eqv (0x90,0x00,0x03) ) {say
 >>>>> "y";} else {say "n"}; $handle.close;'
 >>>>> Buf[uint8]:0x<4D 5A 90 00 03>
 >>>>> y
 >>>>>
 >>>>> $ p6 'my $handle=open("filever.exe", :bin, :ro); my Buf $b; $b=
 >>>>> $handle.read(5); say $b; if ( $b[2..4] eqv (0x90,0x00,0x04) ) {say
 >>>>> "y";} else {say "n"}; $handle.close;'
 >>>>> Buf[uint8]:0x<4D 5A 90 00 03>
 >>>>> n
 >>>>
 >>>>
 >>>> How do I find the position of a pattern in a Buf?
 >>>
 >>> Need `pos` for Buf
 >>
 >> Actually I do believe it is the binary equivalent of `index` I
 >> am looking for
 >>

On 2/2/19 6:09 AM, Brad Gilbert wrote:
>      sub buf-index ( Buf $buf, +@match ) {
>          my $elems = @match.elems;
>          $buf.rotor( $elems => 1 - $elems ).first(* eqv @match.List, :k)
>      }
> 
>      my $buf = Buf[uint8].new(0x4D, 0x5A, 0x90, 0x00, 0x03);
> 
>      say buf-index( $buf, (0x90, 0x00, 0x03)); # 2

Hi Brad,

Thank you!

Did you forget the "return" in the sub?

And, would this be faster or slower than converting to
a string and using "index"?

-T

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About