This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-06-05 21:19:47

nuckelavee
Scratcher
Registered: 2011-05-18
Posts: 79

Variables into lists.

Hi, I'd like to know how to put each letter of a variable into separate items on a list.  Like, to take "John" and turn it into "J, o, h, n", so that I can make acronyms for anything in my new project that I am working on.


Scratch 2.0- announced April 19, 2010, and now in alpha!  It looks cool, and finally has cloning  smile  Yay.

Offline

 

#2 2011-06-05 21:30:18

Harakou
Community Moderator
Registered: 2009-10-11
Posts: 1000+

Re: Variables into lists.

This should do what you're looking for:
(Edit: MoreGamesNow made me realize my script could be simplified, so here's a better version:)

http://i.imgur.com/xnTF6.gif

Last edited by Harakou (2011-06-05 21:47:27)


http://www.blocks.scratchr.org/API.php?action=random&return=image&link1=http://i.imgur.com/OZn2RD3.png&link2=http://i.imgur.com/duzaGTB.png&link3=http://i.imgur.com/CrDGvvZ.png&link4=http://i.imgur.com/POEpQyZ.png&link5=http://i.imgur.com/ZKJF8ac.png

Offline

 

#3 2011-06-05 21:43:33

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: Variables into lists.

Yeah, Harakou script is nice and simple and effective.  Many programming languages come with a function/method that does that, but Scratch doesn't have one that is pre-made, so you have to do it manually.

Alternatively, instead of using a repeat until loop, use a repeat for loop, and put the length of the variable in there.  The rest of the script would be the same.  This is just a tad simpler.

Code:

<When Green Flag Pressed>
<set{ i }to( 0 )>
<Repeat ( length of variable )>
     <change{ i }by( 1 )>
add (letter(i) of (variable)) to [list]>
<end>

Last edited by MoreGamesNow (2011-06-05 21:43:46)


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#4 2011-06-08 12:51:02

nuckelavee
Scratcher
Registered: 2011-05-18
Posts: 79

Re: Variables into lists.

I can't see that image (k9 web security blocks it, for "open image/media search"), but thanks, Both will work.


Scratch 2.0- announced April 19, 2010, and now in alpha!  It looks cool, and finally has cloning  smile  Yay.

Offline

 

Board footer