bodhilogic

bodhilogic

Problem with autoconversion to character

I am pulling data from a db table. A sample record looks like this: [{2019, 3, 1}, 12].
I am doing a [head|tail] on the record so that I can process it.
The head is fine - I can do what I need to do with it.

The problem is the tail (the 12).

It is being processed by the code as '\f' but my code needs a real number, not the character version of it.

?tail works great in iex and IO.inspect but it doesn’t work inside of code.

How can I get either, the number 12 or at worst, the string of the number "12"?

I could cheat and pull that field as a string, but I’d be missing a ‘teaching moment’ (and its a bit of a flaky work-around)

Thank you for any help you can provide!

Marked As Solved

sribe

sribe

You already do have the number 12, or a list with the single element of a number 12. You’re being confused by how it displays in the debugger.

That error indicates that you’re sending a list of a single integer to byte_size, which expects a binary.

But the beginning of you message indicates that you should have a number, not a list. So we need to see more of your code for context about what you’re actually trying to do with your data…

Also Liked

NobbZ

NobbZ

Just as a note a side, you can just do {year, month, day} = date, no need to convert the tuple to a list first. Pattern matching is not limited to lists.

sribe

sribe

ah, you’re being tripped by a subtlety of pattern matching on lists, remember that a linked list is a list of lists, so tail is always a list, possibly empty (well, except for the empty list which has no tail)–here, check out this:

iex(1)> [date|val] = [1,2]
[1, 2]
iex(2)> date
1
iex(3)> val
[2]
iex(4)> [date|[val]] = [1,2]
[1, 2]
iex(5)> date
1
iex(6)> val
2
# the way you really want to do it:
iex(7)> [date,val] = [1,2]
[1, 2]
iex(8)> date
1
iex(9)> val
2

Also

dat = Tuple.to_list(date)
[year, month, day] = dat

should just be {year, month, day} = date

and pretty sure you need Integer.to_string(val)

[EDIT] and I see you’re already calling Integer.to_string–it was just “below the fold” of your message when I was composing this :wink:

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

Other popular topics 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
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

We're in Beta

About us Mission Statement