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

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
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
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
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
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
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
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

We're in Beta

About us Mission Statement