DimWell

DimWell

How do I pattern match part of string as bitstrings?

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 ?

Marked As Solved

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.

Also Liked

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.

hugolnx

hugolnx

@DimWell Just for the record, what you were trying to do is possible, you just need to use the suffix ‘bits-’ or ‘bytes-’…

<<x::bits-size(16), _::binary>> = "true"
inspect(x) # "tr"

<<x::bytes-size(2), _::binary>> = "true"
inspect(x) # "tr"

;D

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")

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48342 226
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement