How can I get a list from state and modify it in qore?

Cristian Baciu

I am using the ServiceApi::saveStateData({"my-key": some_list}); method to save a list to my qorus IE state. What I wanted to ask is how can I get the list from the state and add/remove elements from the list and put it back in the state? Also I want to know how can I join lists? Like for example I want to join two lists under some_list and put it under state?

Thanks a bunch for the help in advance, Cristi

David Nichols

Lists are always passed by reference, and even though there is a "pseudo-class" for lists (as with other basic types in Qore), all the methods are read-only.

To make changes to a list, use the +=, +, push, splice, extract operator, unshift, and pop operators.

Q: How can I add/remove elements from the list?

Option 1: Use the extract operator to remove elements:

prompt$: qore -ne 'list<auto> l = (1, 2, 3, 4); printf("removed: %y\n", (extract l, 1, 2)); printf("l: %y\n", l);'
removed: [2, 3]
l: [1, 4]

Option 2: Use the remove operator with a range to remove a slice of a list:

prompt$ qore -ne 'list<auto> l = (1, 2, 3, 4); printf("removed: %y\n", remove l[1..2]); printf("l: %y\n", l);'
removed: [2, 3]
l: [1, 4]

Q: How can I join lists?

Use the +=, + to concatenate lists:

prompt$ qore -ne 'list<auto> l1 = (1, 2); list<auto> l2 = (3, 4); printf("new list: %y\n", l1 + l2);'
new list: [1, 2, 3, 4]
prompt$  qore -ne 'list<auto> l1 = (1, 2); list<auto> l2 = (3, 4); l1 += l2; printf("new list: %y\n", l1);'
new list: [1, 2, 3, 4]

Note that to add a list as a single element to another list, it's best to use the push operator:

qore -ne 'list<auto> l1 += (1, 2); list<auto> l2 = (3, 4); push l1, l2; printf("new list: %y\n", l1);'
new list: [1, 2, [3, 4]]

Note that I used += for the original assignment above to ensure that l1 has type list<auto> - as a simple assignment would have resulted in list<int> which would cause the push expression to throw an exception.

Also note that as lists are basic types in Qore, they are always passed by value (technically they are passed by reference using copy-on-write semantics), and the "pseudo-class" for lists (as with all pseudo-classes in Qore) implements only read-only methods; updating lvalues in Qore is only done with operators. This is because updating lvalues in Qore is complex due to Qore's multithreaded nature. All Qore operators are thread-atomic, and lvalues can only be changed with operators that guarantee consistency and atomicity even in complex expressions.

The exception to the above is in objects, which are always passed by reference (actually technically with a copy of a reference, similar to Java); all other value types are passed by value.

Este artigo é coletado da Internet.

Se houver alguma infração, entre em [email protected] Delete.

editar em
0

deixe-me dizer algumas palavras

0comentários
loginDepois de participar da revisão

Artigos relacionados

How do I get shuffle() from Collections to not modify every array in a list?

How can I get tags of 0/1 from list of names?

How can I get element from list in JSTL (not forEach)?

How can I get a ranked list from a dictionary?

How can I get keys from list of values?

How can I get multiple output from a list

How can i get List of data from controller

I have a list, how can I split words in list to get each letter from list

How can I modify the NLTK the stop word list in python?

StateError (Bad state: field does not exist within the DocumentSnapshotPlatform). How can I get a doc from firestore?

How and where can I modify the auto generated query from DataAdapter?

How can I get the real image value from each item in my list and subscribe it to another list?

How can I get a list of all the messages in a channel and then pick a random message from that list to send as a message?

How can i get in jQuery the current state of an infinite animation in CSS

How can I get the creator of a State for Hyperledger Fabric in chaincode?

How can I generate a dictionary from a list?

How do I get the state from useSelector after dispatch is done?

How can I get items from a List element for dropdown menu for Spring forms?

How can I get a list of image URLs from a Markdown file in Python?

how I can get the value from datalist

How can I get an array from an If statement?

How I can get entitymanager from crudrepository

How can I get value from GroupedObservable?

Can I modify the workspace files from an extesion

How to share a list of objects without giving ability to modify their state?

In Azure DevOps, how can I automatically move a Work Item from State to State?

How can I get more detailed information out of CloudFormation's "modify" changesets?

How can I modify the map of a Java IndexColorModel?

How can I modify the log format of Katip?

TOP lista

quentelabel

Arquivo