sabo66

sabo66

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

Showing Posts 1 to 10

dimitarvp

dimitarvp

What problem are you looking to solve?

There are many better ways compared to using global variables.

sabo66

sabo66 OP

I’m parsing a CSV file and I would set a global state when catch a specific CSV line

dimitarvp

dimitarvp

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.

sabo66

sabo66 OP

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}

dimitarvp

dimitarvp

That’s an incomplete example.

  1. Show us the actual CSV, not the partially parsed results, please.
  2. Show us how must the output change in the lines 2 and 3 depending on the value of line 1.

Then we would be able to help.

sabo66

sabo66 OP

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”}

LostKobrakai

LostKobrakai

Sounds like a usecase for Enum.chunk_while or the Stream.chunk_while counterpart.

sabo66

sabo66 OP

I will take a look thank you :slight_smile:

dimitarvp

dimitarvp

So B is parent of 05? And C is parent of 10?

sabo66

sabo66 OP

Yes, each line or number (with length > 1) is child of the closest previous letter

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 92995 915
New
AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4616 55835 594
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
matt-savvy
Is there a word for the ~> symbol used in Version strings? Do you also just call it a Squiggle Arrow™ ?!
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews