return array from function

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: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Sjatplat
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 75
Joined: Wed Jun 22, 2011 1:53 pm

return array from function

Post by Sjatplat » Thu Oct 29, 2015 3:47 pm

Hi All

Is it possible to return an Array from a custom function? The reason is that I store my multidimensional array in a custom property. And I want to access that array without having to write

Code: Select all

Put the myArray of this stack into tArray
The following code works:

Code: Select all

on mouseUp
put the keys of cArray()
end mouseUp

function cArray
put the myArray of this stack into pArray
return pArray
end function
but this does not:

Code: Select all

on mouseUp
put cArray()[1]["Text"]
end mouseUp

function cArray
put the myArray of this stack into pArray
return pArray
end function
Hope you understand what I´m trying to do. Any other solution?

Sjatplat

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: return array from function

Post by bn » Thu Oct 29, 2015 4:25 pm

Hi Sjat,

you have to pass parameters to the function in order to work

try this

Code: Select all

on mouseUp
put cArray(2,"Text") into field 1
end mouseUp

function cArray pKey1, pKey2
   put the myArray of this stack into pArray
   return pArray[pKey1][pKey2]
end cArray
Kind regards
Bernd

Sjatplat
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 75
Joined: Wed Jun 22, 2011 1:53 pm

Re: return array from function

Post by Sjatplat » Thu Oct 29, 2015 5:55 pm

Bernd, wonderful. Always a help. Thank you.

Could I add another question....

Is it possible to use this to SET an element in an array with a custom command?
Since the "the paramcount" does not work in a custom command how is it possible to find out how many parameters was passed?
The following code does not work but is more of an example of what i want do to:

Code: Select all

on mouseup
 setMyArray(1,"a new text")
end mouseup 

on setMyArray pKey1,pKey2,pKey3
 put the storedArray of this stack into tArray
 
   switch the paramcount
      case 1
         put pKey1 into tArray[element1]
         break
      case 2
         put pKey2  into tArray[element1][element2]
         break
      case 3
         put pKey3  into tArray[element1][element2][element3]
         break 
   end switch

end setMyArray


Sjat

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: return array from function

Post by bn » Thu Oct 29, 2015 6:16 pm

Sjat,

do you want to add keys or a value to your array?

Bernd

Sjatplat
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 75
Joined: Wed Jun 22, 2011 1:53 pm

Re: return array from function

Post by Sjatplat » Thu Oct 29, 2015 6:25 pm

Bernd, values

in that regard, my example should look more like this:

Code: Select all

on mouseup
setMyArray(1,"a new text")
end mouseup 

on setMyArray pValue1,pValue2,pValue3
put the storedArray of this stack into tArray

   switch the paramcount
      case 1
         put pValue1 into tArray[key1]
         break
      case 2
         put pValue2  into tArray[key1][key2]
         break
      case 3
         put pValue3  into tArray[key1][key2][key3]
         break 
   end switch

end setMyArray

Sjatplat
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 75
Joined: Wed Jun 22, 2011 1:53 pm

Re: return array from function

Post by Sjatplat » Thu Oct 29, 2015 7:48 pm

Bernd, I found a solution

Since "the Paramcount" only returns 1 in a custom command - which is strange...(correct me if Im wrong)..I use the params() instead

This is the fix to my previous code example:

Code: Select all



on setMyArray pKey1,pKey2,pKey3
put the storedArray of this stack into tArray

set itemdel to comma
put the num of items of replaceText(last word of params(),quote,"") into tParams

   switch tParams
      case 1
         put pKey1 into tArray[element1]
         break
      case 2
         put pKey2  into tArray[element1][element2]
         break
      case 3
         put pKey3  into tArray[element1][element2][element3]
         break 
   end switch

end setMyArray
Thank you, Bernd, for guiding me to this solution

kind regards
Sjatplat

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7389
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: return array from function

Post by jacque » Fri Oct 30, 2015 8:14 pm

Sjatplat wrote:Since "the Paramcount" only returns 1 in a custom command - which is strange...(correct me if Im wrong)
Paramcount returns the number of parameters that were passed to the command or function. It doesn't reflect the number of parameters the command expects (the ones in the declaration,) only those it actually receives. For example:

Code: Select all

on test pParam1
  put the paramcount
end test
Type "test" into the message box and you will see 0 because no params were passed. Now type:

test "paramOne","paramTwo"

And you'll get 2 because that's how many you sent.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply