How to add element add the end of an array?

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

Post Reply
Zax
Posts: 118
Joined: Mon May 28, 2007 10:12 am
Location: France

How to add element add the end of an array?

Post by Zax » Mon May 13, 2019 3:55 pm

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:

Code: Select all

$myArray[] = $newElement;
Or to Applescript:

Code: Select all

set end of myList to newItem
Thank you.

Klaus
Posts: 11197
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: How to add element add the end of an array?

Post by Klaus » Mon May 13, 2019 4:30 pm

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

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1033
Joined: Thu Apr 11, 2013 11:27 am

Re: How to add element add the end of an array?

Post by LCMark » Mon May 13, 2019 4:33 pm

@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 ]
Last edited by LCMark on Mon May 13, 2019 5:16 pm, edited 3 times in total.

Klaus
Posts: 11197
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: How to add element add the end of an array?

Post by Klaus » Mon May 13, 2019 4:46 pm

Not rather:

Code: Select all

command pushOntoBackOfArray @xArray, pElement
  put pElement into xArray[the number of elements in xArray + 1]
end pushOntoBackOfArray
? 8)

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1033
Joined: Thu Apr 11, 2013 11:27 am

Re: How to add element add the end of an array?

Post by LCMark » Mon May 13, 2019 4:49 pm

@Klaus - indeed - corrected :)

Klaus
Posts: 11197
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: How to add element add the end of an array?

Post by Klaus » Mon May 13, 2019 4:53 pm

Almost, try again! :D
Hint: xList <> xArray

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6833
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: How to add element add the end of an array?

Post by FourthWorld » Mon May 13, 2019 9:07 pm

@LCMark: Any chance we'll see indexed arrays as an option in LCS?
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

Zax
Posts: 118
Joined: Mon May 28, 2007 10:12 am
Location: France

Re: How to add element add the end of an array?

Post by Zax » Tue May 14, 2019 8:13 am

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... :oops:

zaxos
Posts: 222
Joined: Thu May 23, 2013 11:15 pm
Location: Greece

Re: How to add element add the end of an array?

Post by zaxos » Tue May 14, 2019 11:07 am

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.
Knowledge is meant to be shared.

Zax
Posts: 118
Joined: Mon May 28, 2007 10:12 am
Location: France

Re: How to add element add the end of an array?

Post by Zax » Tue May 14, 2019 12:04 pm

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.

Zax
Posts: 118
Joined: Mon May 28, 2007 10:12 am
Location: France

Re: How to add element add the end of an array?

Post by Zax » Tue May 14, 2019 12:15 pm

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6833
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: How to add element add the end of an array?

Post by FourthWorld » Thu May 16, 2019 5:28 pm

The source XML example above contains only pop and gdp elements, with no name. How do you match the names with that data?
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

Zax
Posts: 118
Joined: Mon May 28, 2007 10:12 am
Location: France

Re: How to add element add the end of an array?

Post by Zax » Fri May 17, 2019 8:53 am

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

zaxos
Posts: 222
Joined: Thu May 23, 2013 11:15 pm
Location: Greece

Re: How to add element add the end of an array?

Post by zaxos » Wed May 22, 2019 9:20 am

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.
Knowledge is meant to be shared.

Zax
Posts: 118
Joined: Mon May 28, 2007 10:12 am
Location: France

Re: How to add element add the end of an array?

Post by Zax » Thu May 23, 2019 8:22 am

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"

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”