Hello I’m new in Elixir, it is possible to assign a global variable from a local scope ?
ex
case x do
"text" -> global_var = 1
end
Hello I’m new in Elixir, it is possible to assign a global variable from a local scope ?
ex
case x do
"text" -> global_var = 1
end
What problem are you looking to solve?
There are many better ways compared to using global variables.
I’m parsing a CSV file and I would set a global state when catch a specific CSV line
That seems unnecessary. Can you post an excerpt from the CSV file and tell us what you are trying to do with it exactly? Like why would you need global state in the first place.
Ok, for exemple I have
A => AGRICULTURE, SYLVICULTURE ET PÊCHE
01 => Culture et production animale, chasse et services annexes
011 => Cultures non permanentes
I would to memorise the “A” in the first line because he is not include in the next lines 2, 3, … and need to memorise this information for make insert in Database %{letter: x, number: y}
That’s an incomplete example.
Then we would be able to help.
file.csv (~2000 lines)
A => AGRICULTURE, SYLVICULTURE ET PÊCHE
01 => Culture et production animale, chasse et services annexes
011 => Cultures non permanentes
0111 => Culture de céréales (à l'exception du riz), de légumineuses et de graines oléagineuses
0111Z => Culture de céréales (à l'exception du riz), de légumineuses et de graines oléagineuses
0112 => Culture du riz
0112Z => Culture du riz
....
B => INDUSTRIES EXTRACTIVES
05 => Extraction de houille et de lignite
051 => Extraction de houille
0510 => Extraction de houille
0510Z => Extraction de houille
052 => Extraction de lignite
0520 => Extraction de lignite
0520Z => Extraction de lignite
06 => Extraction d'hydrocarbures
061 => Extraction de pétrole brut
0610 => Extraction de pétrole brut
0610Z => Extraction de pétrole brut
062 => Extraction de gaz naturel
0620 => Extraction de gaz naturel
....
C => INDUSTRIE MANUFACTURIÈRE
10 => Industries alimentaires
101 => Transformation et conservation de la viande et préparation de produits à base de viande
1011 => Transformation et conservation de la viande de boucherie
1011Z => Transformation et conservation de la viande de boucherie
1012 => Transformation et conservation de la viande de volaille
1012Z => Transformation et conservation de la viande de volaille
1013 => Préparation de produits à base de viande
1013A => Préparation industrielle de produits à base de viande
1013B => Charcuterie
I would capture the informations for when the length of the key is > 1 like this
%{letter: previous_letter, number: number, text: text}
ex
for the line 2 %{letter: “A”, number: 01, text: “Culture et production animale, chasse et services annexes”}
for the line 1 %{letter: “A”, number: nil, text: “AGRICULTURE, SYLVICULTURE ET PÊCHE”}
Sounds like a usecase for Enum.chunk_while
or the Stream.chunk_while
counterpart.
I will take a look thank you
So B is parent of 05? And C is parent of 10?
Yes, each line or number (with length > 1) is child of the closest previous letter
So you want a tree, got it.
Though I still have to point out that the file you have pasted does not look like CSV.
And I can show you how I would solve this but still – what else have you tried? F.ex. do you know about Enum.reduce_while
?
Yes
Yes it’s a CSV the separator is ‘=>’
I’m new in Elixir and functional programming, I will looking Enum.chunk_while and I don’t know yet Enum.reduce_while I will also look this function thank you
That’s… not a thing.
The strict definition of a CSV would be a file that conforms to RFC4180 - comma delimited, fields with commas escaped with "
etc
The “loose” definition expands that to using single characters other than ,
(tabs, pipes, etc) as a delimiter.
But saying “the delimiter is =>
” stretches that definition beyond reason.
Regarding your original question, +1 for starting with Stream.chunk_while
. A possible sequence of operations could look like this:
Stream.chunk_while
to bring together each line starting with a letter and all the following lines that start with a numberStream.flat_map
over each of those chunks:
number: nil
for the line that starts with the letterOh ok ! thank you for the correction
Yes it’s what I’m trying to get, I will try that thank you very much
To answer your specific question: no, you cannot assign a value to a variable in a higher scope
So even if it was a good idea, you couldn’t do it.
Ok thank you
There is no can’t in elixir. With the help of some nasty macros everything is possible .
Oh my god, love it, especially the multiple inheritance part!
Finally I can write code like a normal human being.