Repeat and Arrays

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
W_F_Simons
Posts: 4
Joined: Tue Jul 24, 2012 2:43 am

Repeat and Arrays

Post by W_F_Simons » Tue Jul 24, 2012 3:19 am

I have been dabbling with Live Code for a while now, but can never figure out how to maximize the use of "repeat" and "arrays." What I am trying to do is to calculate the sum of a word, based on letter values, for each word in a list and then populate the value in an adjacent column.

My approach
Repeat for each word in list 1~n,
Put first word into an array,
Repeat for each letter in word
Put each letter into a nested array,
Assign a value to each letter (If A=1 then put 1 into tNestedArray(Char1) / else if B=4 then put 4 into tNestedArray(Char 1), etc end if(I think that I need to create a function or custom property))
Sum the values to each letter,
Then tab and put a value as item 2
end repeat for each letter
end repeat for each word

My code so far:
if script don't work again then
put computer in trash
end if

Help is needed.

Cheers!

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Repeat and Arrays

Post by Mark » Tue Jul 24, 2012 9:55 am

Hi,

This script assumes that you have one word on each line in field x. It also needs a second field y.

Code: Select all

on mouseUp
  put fld x into myList
  repeat for each line myWord in myList
    put 0 into myWordValue
    repeat for each char myChar in myWord
      add valueForChar(myChar) to myWordValue
    end repeat
    put myWordValue into myNewArray[myWord]
  end repeat
  combine myNewArray by return and tab
  put myNewArray into fld y
end mouseUp

function valueForChar theChar
  // do this yourself
end valueForChar
Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

W_F_Simons
Posts: 4
Joined: Tue Jul 24, 2012 2:43 am

Re: Repeat and Arrays

Post by W_F_Simons » Tue Jul 24, 2012 11:37 am

Mark,

Thank you so much... I am going to try it first thing this morning.

Great Regards.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10321
Joined: Wed May 06, 2009 2:28 pm

Re: Repeat and Arrays

Post by dunbarx » Tue Jul 24, 2012 4:59 pm

Mark. W.F

I think there is a typo in the script. Change "line" to "word" in:

repeat for each line myWord in myList

Craig Newman

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Repeat and Arrays

Post by Mark » Tue Jul 24, 2012 6:03 pm

Craig, afaik there is a list of words, one word per line. The script should make sense as I wrote it. I'd expect OP to adjust the script anyway. Maybe s/he'll decide to make the change you suggest.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

W_F_Simons
Posts: 4
Joined: Tue Jul 24, 2012 2:43 am

Re: Repeat and Arrays

Post by W_F_Simons » Tue Jul 24, 2012 6:50 pm

Craig and Mark,

First, thank you both for your quick responses.

I ran it both ways (repeat for each line myWord in myList; and, repeat for each word myWord in myList)... and both ways return the same results.

Again... I greatly appreciate it!!!

Bill

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10321
Joined: Wed May 06, 2009 2:28 pm

Re: Repeat and Arrays

Post by dunbarx » Tue Jul 24, 2012 11:37 pm

Mark.

Seems you were right. ..."each word in a list..." meant one word per line

Craig

Post Reply