Write list

Purpose: Write key/value pair into a linked list.

 write-list <list> key <key> \
     value <value> \
     [ append [ <append> ] ]

write-list adds a pair of key/value strings to the linked <list>, specified with <key> and <value> (in "key" and "value" clauses, collectively called an "element").

The key/value pair is added just prior to the list's current position, thus becoming a current element.

If "append" clause is used without boolean expression <append>, or if <append> evaluates to true, then the element is added at the end of the list, and the list's current element becomes the newly added one.
Examples
Add a key/value pair to the end of the list:
 new-list mylist
 write-list mylist key "mykey" value "myvalue" append

The following is a list that is process-scoped, i.e. it is a linked-list server, which can add, delete, read and position to various elements:
 %% /llsrv
     // Create linked list just once for the life of the process
     do-once
     new-list t process-scope
     end-do-once

     // Get input parameters
     get-param op
     get-param key
     get-param data

     if-true op equal "add" // Add data to list
         // Make a copy of key,data so they are Gliimly-allocated
         write-list t key (key) value data append
         @Added [<<p-out key>>] value [<<p-out data>>]

     else-if op equal "delete" // Delete first data and obtain the value deleted
         position-list t first
         read-list t key (key) value val status st
         if-true st equal GG_ERR_EXIST
             @Not found
         else-if
             // If found, then delete key and value
             @Deleted key [<<p-out key>>], [<<p-out val>>]
             delete-list t
         end-if

     else-if op equal "next" // Position to next element
         position-list t next status st
         if-true st equal GG_OKAY
             @Okay
         else-if
             @Not found
         end-if

     else-if op equal "last" // Position to last element
         position-list t last status st
         if-true st equal GG_OKAY
             @Okay
         else-if
             @Not found
         end-if

     else-if op equal "previous" // Position to element
         position-list t previous status st
         if-true st equal GG_OKAY
             @Okay
         else-if
             @Not found
         end-if

     else-if op equal "first" // Get first element
         position-list t first status st
         if-true st equal GG_OKAY
             @Okay
         else-if
             @Not found
         end-if

     else-if op equal "query" // Get current element
         read-list t key (key) value val status st
         if-true st equal GG_ERR_EXIST
             @Not found
         else-if
             @Key [<<p-out key>>], value [<<p-out val>>]
         end-if

     else-if op equal "purge" // remove all, keep the list
         purge-list t
     end-if
 %%

Create application:
sudo mgrg -i -u $(whoami) linkserver

Start the server:
mgrg -w 1 linkserver

Try it out:
gg -r --req="/llsrv/op=add/key=1/data=1" --exec --service
gg -r --req="/llsrv/op=add/key=2/data=2" --exec --service
gg -r --req="/llsrv/op=query" --exec --service
gg -r --req="/llsrv/op=previous" --exec --service
gg -r --req="/llsrv/op=query" --exec --service

See also
Linked list
delete-list  
get-list  
new-list  
position-list  
purge-list  
read-list  
write-list  
See all
documentation


Copyright (c) 2019-2024 Gliim LLC. All contents on this web site is "AS IS" without warranties or guarantees of any kind.