DimWell

DimWell

Hello Experts,

Could you please advise what would be the way to match the bitstring ?

Lets say I have and input and I want to pattern part of it as bitstsing:

@input    <<116, 114, 117, 101, 32, 105, 115, 32, 110, 111, 116, 32, 102, 97, 108, 115, 101>>
@true_    <<116, 114, 117, 101>>
@false_   <<102, 97, 108, 115, 101>>

def match_bitsting( <<true_ ::size(32) , rest :: bitstring>>= input)  when trues_ == @true_ do
    IO.puts "I got: true"
end

def match_bitsting( <<false_ ::size(40) , rest :: bitstring>>= input)  when trues_ == @false_ do
    IO.puts "I got: false"
end

How should I rewrite @true_ | @false_ or the pattern matching to make it work ?

Showing Posts 1 to 10

grych

grych

Creator of Drab

If I understood the problem properly, you want to match the beginning of the input string? In this case, it is easy:

def match_bitstring("true" <> _rest), do: IO.puts("true")
def match_bitstring("false" <> _rest), do: IO.puts("false")
DimWell

DimWell OP

Not really, probably I have to change description a bit…

I have an input that looks like:

<<116, 114, 117, 101, 32, 105, 115, 32, 110, 111, 116, 32, 102, 97, 108, 115, 101, 32, 97, 110, 100, 32, 102, 97, 108, 115, 101, 32, 105, 115, 32, 110, 111, 116, 32, 116, 114, 117, 101>>

I want to do pattern match on:

<<116, 114, 117, 101>> # which is "true" and the rest data loop froward and match according other principles

Furthermore, this just dummy input just to explain the idea, the input can be any text…

idi527

idi527

@input    <<116, 114, 117, 101, 32, 105, 115, 32, 110, 111, 116, 32, 102, 97, 108, 115, 101>>
@true_    <<116, 114, 117, 101>>
@false_   <<102, 97, 108, 115, 101>>

def match_bitsting(<<@true_, rest::bits>>) do # bits is an alias for bitstring
  IO.puts "I got: true"
  match_continue(rest, true)
end

def match_bitsting(<<@false_, rest::bits>>) do
  IO.puts "I got: false"
  match_continue(rest, false)
end

Would this work for you?

Why are you using bitstrings anyway? What you have in @input is a (printable) binary.

DimWell

DimWell OP

Yes, this works and so clean code :open_mouth:
Somehow I overcomplicated… :hugs:

DimWell

DimWell OP

Trying to understand how the pattern matching works on bitstring level…
I mean how to use pattern matching in this type of scenario…

I am still in learning and and trying to understanding Elixir / Erlang / Pattern Matching

Thank you very much for help !

NobbZ

NobbZ

I think what @idi527 meant here, why do you have bitstring instead of binary. The first one matches arbitrary length sequences of bits while the latter matches arbitrary length sequences of bytes, and since your provided patterns all seem to stick to a byte boundary the latter might perform better then the first.

DimWell

DimWell OP

Hi Noobz

Hm… When I read some online examples it was mentioned that if you are not sure about provided input then it is better to specify bitstring rather than binary

In my case I assume the input data comes from a some API that comes as stream data.
I thought this is the way to do it.

Am I wrong ? :slight_smile: :blush:

NobbZ

NobbZ

If it is streamed data you will probably get your chunks at byte boundary (as this is the level at which your interface transfers the data) and unless the protocol specifies something that is not aligned at byte boundaries, you can safely assume binary.

BUT If the data is streamed in, you can’t safely use binary pattern matching at all but need to use a different mean of parsing/deserializing the incomming data.

What happens if your current chunk is "tru"? This does not match "true", but might after the next chunk has been received.

DimWell

DimWell OP

Yes, you are right. Thank you for a good point. :+1:

DimWell

DimWell OP

I thought to give a try with Genstage & Flow.

Producer:
Something like receives chunks and analyses it according to some logic.
It would split a chunk into invalid and valid parts.

  • The invalid part would buffered until next chunk & analysis.
  • The valid part would be available to ConsumerProducer.

ConsumerProducer:

  • Receives a valid chunk and does the pattern matching.
  • Does some manipulations and prepares data for the next stage.

Consumer:

  • Gets final data produces some output.

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews