Ruud H.G. van Tol on Fri, 24 Nov 2023 09:22:06 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Lookup Table



On 2023-11-24 08:16, Thomas D. Dean wrote:
I want to create a sparse lookup table with a key and value or key and set. When I do table[N], or a function call, I want to get back either 1 or 0. Another use, I want to return a set, {1,2,3,...} or {}.

For example, I want table[N] to return 0 if entry N exists or 1 if it does not.

I tried
? M=Map();
%131 = Map([;])
? mapput(~M,7,0)
? mapget(M,7)
%133 = 0

Does mapget(M,7) use pass by value or is M passed by reference?

? mapget(M,8)
  ***   at top-level: mapget(M,8)
  ***                 ^-----------
  *** mapget: nonexistent component in mapget: index not in map
  ***   Break loop: type 'break' to go back to GP prompt


Is it possible to do this?

For maps, there is mapisdefined( map, key, {&value} ).
And the value can be a set.

See also
https://pari.math.u-bordeaux.fr/pub/pari/manuals/2.15.4/users.pdf

-- Ruud