Page 1 of 1
How to add element add the end of an array?
Posted: Mon May 13, 2019 3:55 pm
by Zax
Hello,
I can't figure how to dynamically add an element at the end of an array.
My array comes from an XML file and looks like this:
Code: Select all
first level[1]
sublevel[1]
sublevel[2]
...
sublevel[n]
first level[2]
...
Each sublevel is also an array, and I would like to add sublevel[n+1] in level[1]
Something equivalent to PHP:
Or to Applescript:
Thank you.
Re: How to add element add the end of an array?
Posted: Mon May 13, 2019 4:30 pm
by Klaus
Bonjour Zax,
I think this will be
...
put the keys of level[1] into tK
put tK + 1 into N
put "whatever" into level1[1]level2[N]
...
Out of my head, so no guarantee!
Best
Klaus
Re: How to add element add the end of an array?
Posted: Mon May 13, 2019 4:33 pm
by LCMark
@Zax: The simplest way to do this is:
put tNewElement into tArray[the number of elements in tArray + 1]
This can be a little bit verbose (particularly if its a deep array) so you can use a simple function to do it:
Code: Select all
command pushOntoBackOfArray @xArray, pElement
put pElement into xArray[the number of elements in xArray + 1]
end pushOntoBackOfArray
[ EDIT: I should point out that the above assumes that xArray is numerically indexed, starting at one and has all indices - which it looks like what you have ]
Re: How to add element add the end of an array?
Posted: Mon May 13, 2019 4:46 pm
by Klaus
Not rather:
Code: Select all
command pushOntoBackOfArray @xArray, pElement
put pElement into xArray[the number of elements in xArray + 1]
end pushOntoBackOfArray
?

Re: How to add element add the end of an array?
Posted: Mon May 13, 2019 4:49 pm
by LCMark
@Klaus - indeed - corrected

Re: How to add element add the end of an array?
Posted: Mon May 13, 2019 4:53 pm
by Klaus
Almost, try again!

Hint: xList <> xArray
Re: How to add element add the end of an array?
Posted: Mon May 13, 2019 9:07 pm
by FourthWorld
@LCMark: Any chance we'll see indexed arrays as an option in LCS?
Re: How to add element add the end of an array?
Posted: Tue May 14, 2019 8:13 am
by Zax
Thanks for your answers.
My array comes from this kind of XML file:
Code: Select all
<countries>
<country>
<pop>12000000</pop>
<gdp>58621</gdp>
</country>
<country>
<pop>5000000</pop>
<gdp>1453613</gdp>
</country>
...
<country>
<pop>24000000</pop>
<gdp>521155</gdp>
</country>
</countries>
The array is populated by revXMLChildNames() function, and is
not numerically indexed.
It looks like:
Code: Select all
countries
country[1]
pop
gdp
country[2]
pop
gdp
...
country[n]
pop
gdp
And I would like add a new "country" at the end of "countries" array.
Code: Select all
command pushOntoBackOfArray @xArray, pElement
put pElement into xArray[the number of elements in xArray + 1]
end pushOntoBackOfArray
As LCMark said, this code only works for numerically indexed arrays.
In the same way, I will also have to
remove a "country" in my array...

Re: How to add element add the end of an array?
Posted: Tue May 14, 2019 11:07 am
by zaxos
Hi Zax,
I will make a guess here that this:
Code: Select all
countries
country[1]
pop
gdp
country[2]
pop
gdp
...
country[n]
pop
gdp
Is not the correct format of your array and is more like this:
Code: Select all
countries
France
pop
gdp
Germany
pop
gdp
...
Greece
pop
gdp
This would make more sense.
In that case you can just do the following to delete a country:
Code: Select all
delete variable tCountriesArray["countries"]["France"]
Or to append data:
Code: Select all
put 6265 into tCountriesArray["countries"]["France"]["gdp"]
Or create a function:
Code: Select all
function editCountriesArray pCommand, @pArray, pCountry, pData
switch pCommand
case "delete"
delete variable pArray["countries"][pCountry]
break
case "appendGdp"
# if pCountry vaiable does not exist, it will be created.
put pData into pArray["countries"][pCountry]["gdp"]
break
case "appendPop"
# if pCountry vaiable does not exist, it will be created.
put pData into pArray["countries"][pCountry]["pop"]
break
end switch
end test
end editCountiesArray
In any case, i don't see why you need to add an element at the end of the array if your array is not numerically indexed. Maybe what you are looking for is a way to sort the array.
Re: How to add element add the end of an array?
Posted: Tue May 14, 2019 12:04 pm
by Zax
Hello zaxos,
Nope, my array looks like I said.
You can try with this script:
Code: Select all
put "<?xml version=" & quote & "1.0" & quote && "encoding=" & quote & "utf-8" & quote && "?><countries>" & \
"<country><name>France</name><pop>12000000</pop></country>" & \
"<country><name>Greece</name><pop>5000000</pop></country>" & \
"<country><name>England</name><pop>24000000</pop></country>" & \
"</countries>" into xmlText
put revXMLCreateTree(XMLText, true, true) into xmlId
if xmlId is an integer then
put revXMLChildNames(xmlId, "countries", return, "country", true) into arrCountries
else answer "Bad XML data."
I built my array according to this LC lesson:
http://lessons.livecode.com/m/4071/l/70 ... n-xml-file
zaxos wrote: ↑Tue May 14, 2019 11:07 am
In that case you can just do the following to delete a country:
Code: Select all
delete variable tCountriesArray["countries"]["France"]
Great! Thank you.
Re: How to add element add the end of an array?
Posted: Tue May 14, 2019 12:15 pm
by Zax
I finally find a dirty solution, but it seems to work:
Code: Select all
on endArray @arr, newElement, elementName
put elementName & "[" & (( the number of elements in arr) + 1) & "]" into nextElementName
put newElement into arr[nextElementName]
end endArray
Re: How to add element add the end of an array?
Posted: Thu May 16, 2019 5:28 pm
by FourthWorld
The source XML example above contains only pop and gdp elements, with no name. How do you match the names with that data?
Re: How to add element add the end of an array?
Posted: Fri May 17, 2019 8:53 am
by Zax
In both examples, function to add en element would be called like this:
Code: Select all
put "45000" into arrNewElement["pop"]
put "22000000" into arrNewElement["gdp"]
endArray arrCountries, arrNewElement, "country"
In example on page
http://lessons.livecode.com/m/4071/l/70 ... n-xml-file
it would be:
Code: Select all
endArray tRecentDocuments, arrNewElement, "recentDocument"
But this function only works if the array to modify is well formated. A safer way is to copy the array:
Code: Select all
on endArray @arr, newElement, elementName
put "" into newArray
put 0 into tCounter
repeat for each element elt in arr
add 1 to tCounter
put elementName & "[" & tCounter & "]" into nextElementName
put elt into newArray[nextElementName]
end repeat
put elementName & "[" & (tCounter + 1) & "]" into nextElementName
put newElement into newArray[nextElementName]
put newArray into arr
end endArray
Re: How to add element add the end of an array?
Posted: Wed May 22, 2019 9:20 am
by zaxos
I think what FourthWorld is saying is that right now you have an array that has its index keys in the following format:
Code: Select all
tArray["country1"]
tArray["country2"]
tArray["country3"]...
That makes no sense since the only way you could add data to this array is either if you knew somehow which country is stored in "country1" key or if you iterate through all the keys with a repeat and find out the country ( which i assume is what you are doing ). I assume you are following that strategy because of the tutorial you've read ( i haven't checked it ) but there is no need to go strictly by it, you can adapt to your needs.
In the example i provided, country name is the index key:
Code: Select all
tArray["France"]
tArray["Greece"]
tArray["Germany"]
So instead of repeating through all the keys to find out where "France" is stored, you can just do:
Code: Select all
put "45000" into tArray["France"]["pop"]
Your xml would look something like this:
Code: Select all
<?xml version="1.0" encoding="utf-8" ?>
<countries>
<France>
<gdp>5000</gdp>
<pop>12000000</pop>
</France>
<Greece>
<gdp>5000</gdp>
<pop>5000000</pop>
</Greece>
<England>
<gdp>5000</gdp>
<pop>24000000</pop>
</England>
</countries>
You don't need to add a "Country" node, you already know these are countries since they are under <countries> node if that's your concern.
Re: How to add element add the end of an array?
Posted: Thu May 23, 2019 8:22 am
by Zax
Sorry if I didn't understand the question.
Let's forget my examples, and go back to my initial question.
Following this XML lesson
http://lessons.livecode.com/m/4071/l/70 ... n-xml-file, we have an array like this:
Code: Select all
recentDocuments
recentDocument[1]
recentDocument[2]
recentDocument[3]
I didn't realize integers between brackets was treated as string, and this kind of array was an fully associative array. I wanted to add the element
recentDocument[4], so I finally wrote this command:
Code: Select all
on endArray @arr, newElement, elementName
put "" into newArray
put 0 into tCounter
repeat for each element elt in arr
add 1 to tCounter
put elementName & "[" & tCounter & "]" into nextElementName
put elt into newArray[nextElementName]
end repeat
put elementName & "[" & (tCounter + 1) & "]" into nextElementName
put newElement into newArray[nextElementName]
put newArray into arr
end endArray
In the above example, this command is called with:
Code: Select all
endArray tRecentDocuments, arrNewElement, "recentDocument"