Trying to make Array work in repeat

LiveCode Builder is a language for extending LiveCode's capabilities, creating new object types as Widgets, and libraries that access lower-level APIs in OSes, applications, and DLLs.

Moderators: LCMark, LCfraser

Post Reply
jun
Posts: 10
Joined: Mon May 27, 2013 7:44 am
Contact:

Trying to make Array work in repeat

Post by jun » Tue Dec 22, 2020 1:55 pm

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)

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3459
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Trying to make Array work in repeat

Post by bn » 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

PaulDaMacMan
Posts: 341
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Trying to make Array work in repeat

Post by PaulDaMacMan » Tue Dec 22, 2020 10:04 pm

@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.
My LCB Repos: https://github.com/PaulMcClernan/

jun
Posts: 10
Joined: Mon May 27, 2013 7:44 am
Contact:

Re: Trying to make Array work in repeat

Post by jun » Wed Dec 23, 2020 7:04 am

@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

jun
Posts: 10
Joined: Mon May 27, 2013 7:44 am
Contact:

Re: Trying to make Array work in repeat

Post by jun » Wed Dec 23, 2020 12:54 pm

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.

PaulDaMacMan
Posts: 341
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Trying to make Array work in repeat

Post by PaulDaMacMan » Fri Jan 08, 2021 1:33 am

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
My LCB Repos: https://github.com/PaulMcClernan/

Post Reply

Return to “LiveCode Builder”