Bill Allombert on Fri, 24 Nov 2023 09:21:33 +0100


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

Re: Lookup Table


On Thu, Nov 23, 2023 at 11:16:35PM -0800, 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?

It is semantically passed by value, but it is not copied.

> ? mapget(M,8)
>   ***   at top-level: mapget(M,8)
>   ***                 ^-----------
>   *** mapget: nonexistent component in mapget: index not in map
> 
> Is it possible to do this?

Use mapisdefined(M,8)

Cheers,
Bill.