Page 1 of 1

return array from function

Posted: Thu Oct 29, 2015 3:47 pm
by Sjatplat
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

Re: return array from function

Posted: Thu Oct 29, 2015 4:25 pm
by bn
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

Re: return array from function

Posted: Thu Oct 29, 2015 5:55 pm
by Sjatplat
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

Re: return array from function

Posted: Thu Oct 29, 2015 6:16 pm
by bn
Sjat,

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

Bernd

Re: return array from function

Posted: Thu Oct 29, 2015 6:25 pm
by Sjatplat
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

Re: return array from function

Posted: Thu Oct 29, 2015 7:48 pm
by Sjatplat
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

Re: return array from function

Posted: Fri Oct 30, 2015 8:14 pm
by jacque
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.