Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erlang - Global Variables .. yes I know, I know

Tags:

global

erlang

Ok I have been trying every which way to figure this out.

I need to get this table to be a global.. I have realized it is much less efficient to pass TableID around.. in the scope of my program.

So I tried creating a new table then looking it up:

TableID = ets:new(tb, [set,public]),
put({tableUniqueID}, TableID),

Then I used:

get({tableUniqueID})

And in the same function it returns TableID just fine... yet when I use it in another function, it returns an undefined.

What?? I thought get and put made a key global..

ALSO before all this I realized you "could" call a table lookup function as such:

ets:lookup(get({tableUniqueID}), msgIn)

Same thing, in function works, outside does not.. Get Put problem..

Then I realized another way to Lookup a table would be by calling the atom of the table

ets:lookup(tb, msgIn)

But this NEVER works, not inside the function, not out..

So my main priority would be to figure why looking up a table by its atom, is not possible. But it says it is most everywhere, including the manual.

The get/put I could live without, As long as I can store table, then lookup the table by its atom identifier.

Can anyone shed light on this dilemma?

like image 665
BAR Avatar asked Aug 16 '10 04:08

BAR


People also ask

How do I create a global variable in Erlang?

In Erlang, all the variables are bound with the '=' statement. All variables need to start with the upper case character. In other programming languages, the '=' sign is used for the assignment, but not in the case of Erlang. As stated, variables are defined with the use of the '=' statement.

How do you announce a global variable?

The keyword global is used when you need to declare a global variable inside a function. The scope of normal variable declared inside the function is only till the end of the function. However, if you want to use the variable outside of the function as well, use global keyword while declaring the variable.

How do you check globals?

To check if a global variable exists in Python, use the in operator against the output of the globals() function, which has a dictionary of a current global variables table. The “in operator” returns a boolean value. If a variable exists, it returns True otherwise False.


1 Answers

I GOT IT!!

Wish the docs, would say this under the lookup function.. Better yet, everyone who writes tutorials on ets, or more so books

The solution is to

TableID = ets:new(tb, [set,public,named_table])

That named_table is the important part

Some digging through man pages, but

;)

like image 105
BAR Avatar answered Sep 23 '22 03:09

BAR