SPlit word into list

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
iowgav
Posts: 2
Joined: Fri Sep 27, 2013 1:58 pm

SPlit word into list

Post by iowgav » Fri Sep 27, 2013 2:04 pm

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.

BvG
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1236
Joined: Sat Apr 08, 2006 1:10 pm
Location: Zurich
Contact:

Re: SPlit word into list

Post by BvG » Fri Sep 27, 2013 2:33 pm

There is no built in way to split by char into an array, so you'd need to use a repeat loop:

Code: Select all

repeat for each char myChar in myStoredWord
   put 1 into myArray[myChar]
end repeat
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

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: SPlit word into list

Post by dunbarx » Fri Sep 27, 2013 3:25 pm

What Bjoernke said.

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
Craig Newman

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: SPlit word into list

Post by dunbarx » Fri Sep 27, 2013 3:38 pm

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:

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
This may be more robust for you, though it is just another way to do things, which LC is justly famous for.

Craig Newman

iowgav
Posts: 2
Joined: Fri Sep 27, 2013 1:58 pm

Re: SPlit word into list

Post by iowgav » Fri Oct 04, 2013 2:29 pm

Many thanks for all the replies. I will have a go and see which will work best.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”