lib for developers : libAssoc
|
09-17-2017, 04:09 PM
(This post was last modified: 09-17-2017 05:32 PM by primer.)
Post: #1
|
|||
|
|||
lib for developers : libAssoc
Hello,
here is a library about list processing, it add the Associative list concept. What is "Associative list" ? It's powerfull list that you can reach item by a key instead of an index. You can store value based on name, for example list of GDP per country name... This feature is well known in php, here is an example of php associated list : PHP Code: $mylist= ['key1'=>value1, 'otherkey'=>value2]; With LibAssoc, it gives : mylist:=AL_NEW(); //create the AList object AL_SET(mylist,"key1",value1); //insert first value, for "key1" name AL_SET(mylist,"key2",value2); AL_GET(mylist,"key1"); // returns value1 Please note as Associative List is not a buildin object, most of API functions require the AList object to be passed as first parameter. Note that 3 first lines can be shortened by this : Code: mylist:=AL_SPLIT("key1=value1;key2=value2",";","="); Please note this lib has one dependency : LibList, please first download it. Download LibAssoc v0.801 - sept 2017: LibAssoc.hpprgm (Size: 10.07 KB / Downloads: 10) API list : AL_NEW, AL_SET, AL_GET, AL_FKEY, AL_SIZE, AL_DEL, AL_VMATCH, AL_VMAP, AL_FOREACH, AL_SPLIT AL_NEW() Create a new Associative List ex : Code: L:=AL_NEW(); AL_SET(AList,Key,Val) set/insert key+value in AList AList is an associtive list, Key is a string, Val any type you want. ex : Code: L:=AL_NEW(); AL_GET(AList,Key) Return value from a given Key if not found, return an empty string. ex : Code: V:=AL_GET(L,"key1"); AL_FKEY(AList,Key) is Key exist on AList ? return non null if exist, 0 if not ex : Code: if AL_FKEY(L,"key1") then ... AL_SIZE(AList) Returns the number of item ex : Code: PRINT("there are "+AL_SIZE(L)+" item(s) "); AL_DEL(AList,Key) Remove an item from a Associative List ex : Code: AL_DEL(L,"key1"); AL_VMATCH(AList,FCT) Value Matching Run a user function Fct on each items, based upon function return, build a subset Assciative List and return it. user function receive item value, and must return >0 to match. ex : Code: L:=AL_NEW(); AL_VMAP(AList,Fct) Run user fuction on each Value AL_FOREACH(AList,Fct) Run user fuction on each (Key,Value) ex : Code:
AL_SPLIT(Str,Sep1,Sep2) Build a AList from a string Sep1 is object separator Sep2 is the key/value separator. ex : Code:
primer |
|||
09-17-2017, 04:51 PM
Post: #2
|
|||
|
|||
RE: lib for developers : libAssoc
nice!
Would be possible to see the source code as well? Wikis are great, Contribute :) |
|||
09-17-2017, 05:26 PM
(This post was last modified: 09-17-2017 05:35 PM by primer.)
Post: #3
|
|||
|
|||
RE: lib for developers : libAssoc
(09-17-2017 04:51 PM)pier4r Wrote: nice!Yes, source code is poorly commented, you have it here : Code: // LibAssoc v0.801 - primer september 2017 primer |
|||
11-10-2020, 01:18 PM
Post: #4
|
|||
|
|||
RE: lib for developers : libAssoc
Hello people,
I found the key/value list a very important resource. Some time ago, I was looking for a solution to save the values of some variables of a program, to be used in future executions of the same program, or of other programs. I tried to use LibAssoc, but I'm not getting it right. Both LibList and LibAssoc are installed on the calculator. I tried to use the L1 list as well as a list previously created by me. Does not work. Does anyone know what the reason is? //Test use of LibAssoc //Marcos Cabral [/code]LibList(); LibAssoc(); display(); EXPORT TESTE() BEGIN LOCAL Size, Position, Key, Value; L1:=AL_NEW(); AL_SET(L1, "Q", 6.2); AL_SET(L1, "V", 2.0); AL_SET(L1, "Y", 0.80); Size:=AL_SIZE(L1); Position:= AL_FKEY(L1,"V"); Value:=AL_GET(L1,"V"); PRINT(); PRINT("Size=" + Size); PRINT("Position=" + Position); PRINT("Value=" + Value); AL_FOREACH(L1, "display"); // show all entries in the list END; EXPORT display(k,v) PRINT(k+" -> "+v); END; |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)