Page 1 of 1

Trying to make Array work in repeat

Posted: Tue Dec 22, 2020 1:55 pm
by jun
So, I am learning livecode builder and I am stuck when I tried to modify the value in my array.
I tried modifying each value on my array by multiplying 10 but I got an error when I tried to call in LC.

Sorry for newbie question.

Code: Select all

public handler testLoop(in pArray as Array) returns Array
  variable tResult as Array  
  variable tKey as optional any    
  repeat for each key tKey in pArray		    
    put pArray[tKey]into tResult[tKey]            	  //THIS WILL WORK
    //put pArray[tKey] * 10 into tResult[tKey]        //ADDING "* 10" > THIS WILL WILL HAVE AN ERROR
  end repeat  
  return tResult
end handler
Here's my sample code in LC

Code: Select all

put 1 into test[0]
put 2 into test[1]
put 3 into test[2]
put testLoop(test) into test1
answer test1[1]
Error:
No matching handler for arguments with types (<type: livecode.lang.string>,<type: livecode.lang.number>,<type: __builtin__.cbool>) - possible handlers (MCArithmeticEvalNumberTimesNumber)

Re: Trying to make Array work in repeat

Posted: Tue Dec 22, 2020 4:47 pm
by bn
Hi Jun,

does

Code: Select all

put (pArray[tKey] parsed  as number) * 10 into tResult[tKey]
work? I could not test it at the moment.

Kind regards
Bernd

Re: Trying to make Array work in repeat

Posted: Tue Dec 22, 2020 10:04 pm
by PaulDaMacMan
@jun no worries, I think we're basicallly all still newbs when it comes to LiveCode Builder

I'm guessing the pArray you are passing in is coming from LiveCode Script? I think LCS stores data in it's arrays a bit differently than LCB. At any rate it is definitely a variable typing error. Keep in mind that maybe the biggest differences between LiveCode Script and LiveCode Builder is that LCB is strictly typed whereas LCS uses dynamic typing and would auto-magically convert a string "1" to an integer 1 versus in LCB you need to specifically convert a string representation of a number to a Numeric type (Number, Integer, NaturalFloat, CSInt, CDouble, etc.) with 'parsed as number' as Bernd suggested.

Re: Trying to make Array work in repeat

Posted: Wed Dec 23, 2020 7:04 am
by jun
@bn Thanks I will try that later. I even tried something like that but I missed the open and clause parenthesis.

@PaulDaMacMan Appreciate the explanation. By the way, I am reading/looking on your repo to get some ideas especially on syntax in LCB. Hopefully you don't mind :D

Re: Trying to make Array work in repeat

Posted: Wed Dec 23, 2020 12:54 pm
by jun
bn wrote:
Tue Dec 22, 2020 4:47 pm
Hi Jun,

does

Code: Select all

put (pArray[tKey] parsed  as number) * 10 into tResult[tKey]
work? I could not test it at the moment.

Kind regards
Bernd

This works great. Thank you.

Re: Trying to make Array work in repeat

Posted: Fri Jan 08, 2021 1:33 am
by PaulDaMacMan

Code: Select all

@PaulDaMacMan Appreciate the explanation. By the way, I am reading/looking on your repo to get some ideas especially on syntax in LCB. Hopefully you don't mind  :D
Mind!?!?! I'm super-happy about it! Encouraging LiveCode-Builder adoption and code-sharing are the very reasons I've put all my LCB stuff on GitHub! :D