Post Reply 
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];
$mylist['key1'// returns values1 

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:

.hpprgm  LibAssoc.hpprgm (Size: 10.07 KB / Downloads: 8)

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_SET(L,"key1","value1");
AL_SET(L,"key2",123); // key2 value is 123.


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_SET(L,"key1",10);
AL_SET(L,"key2",20);

EXPORT myFct(val)
IF val>15 return 1; END;
return 0;
END;

L2:=AL_VMATCH(L,"myFct"); // L2 have only the key2 item. (value>15)


AL_VMAP(AList,Fct)
Run user fuction on each Value


AL_FOREACH(AList,Fct)
Run user fuction on each (Key,Value)
ex :
Code:

EXPORT display(k,v)
 PRINT(k+" -> "+v);
END;

AL_FOREACH(L,"display");


AL_SPLIT(Str,Sep1,Sep2)
Build a AList from a string
Sep1 is object separator
Sep2 is the key/value separator.
ex :
Code:

L:=AL_SPLIT("key1=123|key2=456|key3=789","|","=");

primer
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
lib for developers : libAssoc - primer - 09-17-2017 04:09 PM
RE: lib for developers : libAssoc - pier4r - 09-17-2017, 04:51 PM
RE: lib for developers : libAssoc - primer - 09-17-2017, 05:26 PM



User(s) browsing this thread: 1 Guest(s)