Hi there,
I am a complete NOOB with some Scratch experience but that is where my programming knowledge stops.
I am trying to split a word (in a variable) into its constituent letters and create an array from it.
I cannot find a simple way of doing this in LiveCode. I have looked at the split command but the advice only seems to count with comma or space delimited variables.
Is there a simple way of doing this?
PS. It is for a hangman game.
SPlit word into list
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
-
- VIP Livecode Opensource Backer
- Posts: 1236
- Joined: Sat Apr 08, 2006 1:10 pm
- Location: Zurich
- Contact:
Re: SPlit word into list
There is no built in way to split by char into an array, so you'd need to use a repeat loop:
Note that it's probably easier to use a different approach then an array, depending on what you actually need to do. For example, considering finding out if a char is in a string:
Code: Select all
repeat for each char myChar in myStoredWord
put 1 into myArray[myChar]
end repeat
Code: Select all
if myUserInputChar is in myStoredWord then
--sucessful
else
--hang him!
end if
Various teststacks and stuff:
http://bjoernke.com
Chat with other RunRev developers:
chat.freenode.net:6666 #livecode
http://bjoernke.com
Chat with other RunRev developers:
chat.freenode.net:6666 #livecode
-
- VIP Livecode Opensource Backer
- Posts: 9648
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: SPlit word into list
What Bjoernke said.
Another possibility, depending on what you intended to do with this array:
Craig Newman
Another possibility, depending on what you intended to do with this array:
Code: Select all
on mouseUp
repeat with y = 1 to the number of chars in "myStoredWord"
put char y of mystoredWord into myArray[y]
end repeat
end mouseUp
-
- VIP Livecode Opensource Backer
- Posts: 9648
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: SPlit word into list
I played around a bit , since you had gotten stuck with the delimiters required to use the "split" command. It is true that a living character must be designated as the primary (or secondary) delimiter, and a single word has no such thing. There is nothing at all separating the string of chars in a word. After all, you could not split by empty. (try it)
So please think about doing something like this, just as an exercise:
This may be more robust for you, though it is just another way to do things, which LC is justly famous for.
Craig Newman
So please think about doing something like this, just as an exercise:
Code: Select all
on mouseUp
get "myStoredWord"
repeat for each char tChar in it
put tchar & comma after temp
end repeat
split temp by comma
end mouseUp
Craig Newman
Re: SPlit word into list
Many thanks for all the replies. I will have a go and see which will work best.