Re: pointers to variables ?

new topic     » goto parent     » topic index » view thread      » older message » newer message
cjnwl said...

Another newbie question: are there pointers to variables, or is there any way to simulate them ?

Short answer: no.

Long answer: technically yes, but not in a way that's available to the user. Atoms and sequences are stored in memory and the "value" you're using is actually a pointer. Euphoria uses reference counting with pass-by-value semantics and copy-on-write for modifications. This is why routines like append() return the modified object instead of operating on it directly. Integers are not pointers, are literally signed int values up to "n-bits minus one" i.e. 31 bits on 32-bit systems and 63 bits on 64-bit systems. This is due to the upper bit on integers being used to indicate the value inside is actually pointer to an atom or sequence.

cjnwl said...

For example, in gawk there are no pointers per se, but you can simulate them using the SYMTAB array:

my_list[1] = "test" 
var = "my_list"  
print SYMTAB[var][1] 
This last line is equivalent to
print my_list[1] 

That looks a lot like an associative array? Is that direct use of the symbol table even "legal" in gawk?

cjnwl said...

In my gawk app I make extensive use of this ability as I need to manipulate lists whose name I only know at runtime, and I don't know that I'll be able to do this without pointers or something similar that enables me to access the symbol table.

While Euphoria does have its own symbol table internally, the only user-facing access to it is via routine_id() which is obviously just for routines. There is no equivalent variable_id(). I think this is mainly due to routines being static at runtime. Routines either exist or they don't, and their "value" (the IL code to execute) is constant. Whereas variables are not only dynamic (their "value" can change) but also ephemeral (they can vanish at almost any time). I'm not against adding variable_id() to the interpreter and I actually did that a long long time ago (c. 2005) but I've since lost that code. If I recall correctly, my changes only worked in the interpreter and could cause memory leaks if used improperly. I was pretty new to all this at the time.

cjnwl said...

At the moment I'm thinking that I may be able to use a map of lists as a workaround, but I haven't really gone into my existing gawk code to think it through, and if pointers do exist it would make things much more straightforward.

Yep, you're definitely back to maps. From what you're describing, this seems like basically the same thing:

include std/io.e 
include std/map.e 
 
map datalist = map:new() 
 
map:put( datalist, "countries", 
  io:read_lines("countries.list") 
) 
 
map:put( datalist, "departments", { 
  "HR", 
  "Admin", 
  "Facilities", 
  "Services", 
  "Sales", 
  "IT", 
  "EMT" 
}) 
 
map:put( datalist, "employees", { 
  "joe smith", 
  "andy bloggs", 
  "dave simpson", 
  "ian jones", 
  "wendy carter", 
  "jane allenby" 
}) 
 
sequence employees = map:get( datalist, "employees" ) 
 
-- etc. 

-Greg

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu