PxPlus User Forum

Main Board => Discussions => Language => Topic started by: latcheso on October 25, 2024, 12:14:48 PM

Title: Hash Tables
Post by: latcheso on October 25, 2024, 12:14:48 PM
How would I assign a dynamic array to a hash table?

For example,

//Dynamic Array
DIM VALUES$
VALUES$
VALUES$

//Hash Table
DIM NAMES$

NAMES$["Luke"] = VALUES$

I essentially want

Key "Luke" = [1,2]






Title: Re: Hash Tables
Post by: Stéphane Devouard on October 28, 2024, 11:10:27 AM
Hello

You can't. 
You can only assign a numeric or a string value to an associative array element

Basically, what you want is :

names$["Luke", 1] = "1"
names$["Luke", 2] = "2"

But such a syntax is invalid in PxPlus.

However if what you want is actually to convert the hash table to a JSON string, you can do it like this

->names$["Luke.1"]="1"
->names$["Luke.2"]="2"
->? dim(list edit names$)
{
  "Luke":["1","2"]
}
->
Hope this helps