Page 1 of 1

Concatenating Arrays

Posted: Sat Oct 10, 2015 1:44 pm
by CoreySchroeder
Hey guys,

I'd like to inquire about how I could concatenate arrays within livecode.

Here is what I'm trying to do specifically -

I have several arrays -

aryName - is a string
aryNum1 - is an integer
aryNum2 - is an integer
arySum - is an integer that is the sum of aryNum1 and aryNum2
aryTotal - is an integer that is the sum of all integers stored in arySum

Lets say my input is as follows -

(in the first index of the arrays)
aryName - Bob
aryNum1 - 7
aryNum2 - 7
arySum - (the sum of both, or 14)

(in the second index of the arrays)
aryName - John
aryNum1 - 8
aryNum2 - 2
arySum - (the sum of both, or 10)

aryTotal - the sum of both index values (14+10, or 24)

How could I concatenate this information into a format similar to:

Bob has 7 of the first item and 7 of the second item, or 14 items in total.
John has 8 of the first time and 2 of the second item, or 10 items in total.
The total number of items used is 24.

I've seen a few examples of the combine and split functions, but am not exactly sure how to use them in order to yield the results I am after.
The other thing I would like to mention, is that the number of indexes will be unknown - maybe one time the user may only have 2 as shown in my example above, and maybe another time the user will have 25 - so I will need to find a way to keep track of or query the number of indexes that are currently used in each array.

I really appreciate everyones help on this.

Re: Concatenating Arrays

Posted: Sat Oct 10, 2015 3:21 pm
by dunbarx
Hi.

Do you mean this:

Code: Select all

on mouseUp
   put "Bob" into myArray["Bob"]
   put 7 into myArray["Bob"][aryNumber1]
   put 7 into myArray["Bob"][aryNumber2]
   put myArray["Bob"][aryNumber1] + myArray["Bob"][aryNumber2] into myArray["Bob"][arySum]
   
    put "John" into myArray["John"]
   put 8 into myArray["John"][aryNumber1]
   put 2 into myArray["John"][aryNumber2]
      put myArray["John"][aryNumber1] + myArray["John"][aryNumber2] into myArray["John"][arySum]

put myArray["bob"][arySum] + myArray["John"][arySum] into aryTotal
end mouseUp
It is not clear where aryTotal should go. Step through the handler to see how the array develops. One of the nicest things about the debugger is that is shows array contents line by line.

Craig Newman

Re: Concatenating Arrays

Posted: Sat Oct 10, 2015 4:23 pm
by CoreySchroeder
Hi Craig,

I appreciate the reply and apologize for my inquiry being unclear.
I come from more conventional programming languages, so how LiveCode works with arrays is a bit hard for me to wrap my head around.

a ROUGH example of what I'm trying to do from a more standard programming point of view -

Code: Select all


// declare the variables as empty arrays, and set the final total to 0

var aryName, aryNum1, aryNum2, arySum = []
var aryTotal = 0

// populate the arrays with content

aryName[1] = "Bob"
aryNum1[1] = 7
aryNum2[1] = 7

aryName[2] = "John"
aryNum1[2] = 8
aryNum2[2] = 2

// Populate arySum with the total of both numbers

for (i=0; i <= aryName.length; i++) {
arySum.push( aryNum1[i] + aryNum2[i] )
}

// Get the total of all indexes of arySum and put it into our aryTotal variable

for (i=0; i <= arySum.length; i++) {
aryTotal += arySum[i]
}

// I would then like to concatenate this information into something like the following - note the aryTotal variable will be used for something later.

for (i=0; i <= aryName.length; i++) {
print( aryName[i] + " has " + aryNum1[i] + " of the first item and " + aryNum2[i] + " of the second item, or " + arySum[i] + " items in total." )
}

I guess I'm just confused as to how to keep all of this content isolated into their own array, and not have to create an associative array as your example shows.
(all of the names in one array, all of the num1s in one array, all of the num2s in one array, etc)
And I'm not sure exactly how the combine/split properties would be used in this situation in order to make the code "human readable" for text output, then put back into the array for later code manipulation.

I feel like livecode arrays have been my biggest struggle with learning livecode.
Everything else has been pretty straightforward to learn and understand.
I've read the manual, checked the dictionary, looked at the example scripts, but nothing has really given me the "ah ha!" moment where everything just comes together into a complete understanding.
I feel like once I can master this concept of arrays in livecode - this will really open some doors for me and my app ideas.

I appreciate everyones willingness to help me out - and I apologize for the noob questions - I just really want to get this figured out.

Re: Concatenating Arrays

Posted: Sat Oct 10, 2015 7:06 pm
by sritcp
Hi Corey:

As far as I can tell, LiveCode doesn't support true, "matrix-type", multi-dimensional arrays. You can, of course nest arrays within elements of other arrays.
There is no command to horizontally concatenate arrays.

However, here's how to display individual arrays of name, number, sum, etc. (as in your example) in a user-friendly format:

Code: Select all

on mouseUp
   local tName, tNum1, tNum2, tSum, tAll
   put "Bob" into tName[1]
   put "John" into tName[2]
   put "Jane" into tName[3]
   
   put "7" into tNum1[1]
   put "8" into tNum1[2]
   put "9" into tNum1[3]
   
   put "7" into tNum2[1]
   put "2" into tNum2[2]
   put "3" into tNum2[3]
   
   repeat with i = 1 to the number of lines in the keys of tNum1
      put tNum1[i] + tNum2[i] into tSum[i]
   end repeat
   
   -- put the individual arrays into a larger array
   put tName into tAll[1]
   put tNum1 into tAll[2]
   put tNum2 into tAll[3]
   put tSum into tAll[4]
   
   combine tAll[1] using return
   combine tAll[2] using return
   combine tAll[3] using return
   combine tAll[4] using return
   set columnDelimiter to tab
   combine tAll by column
   
   set the tabStops of field "Field2" to 10,20,30
   put tAll into field "Field2"
   
end mouseUp
The above returns

Bob 7 7 14
John 8 2 10
Jane 9 3 12

which is what you were looking for, I assume.

Regards,
Sri