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

#1 2011-11-08 17:51:12

nit827
New Scratcher
Registered: 2011-11-08
Posts: 1

strings

I was just wondering if you enter a string in Scratch with multiple words seperated by a space how do you break down the words into seperate strings.

Example of a string to break down: Saints 45 Lions 12

Offline

 

#2 2011-11-08 18:16:05

owetre18
Scratcher
Registered: 2009-07-01
Posts: 1000+

Re: strings

There is probably some huge, complicated script to do so, but I don't know it.

Even if I did know it, which I kind of do, a new scratcher might not understand it. Even I did not understand it when I saw it before.

Offline

 

#3 2011-11-08 19:00:45

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

Re: strings

The only way to do it is with a loop and a list, there isn't a "split (string) by (symbol)" block on scratch.

Code:

set [i] to (0)
set [j] to (1)
delete (all) of [list]
add () to [list]
repeat (length of variable)
{
     change [i] by (1)
     if(letter (i) of (variable) == )
     {
          add () to [list]
          change [j] by (1)
     }
     else
     {
          replace item (j) of [list] with  (join ((item (j) of [list]) with (letter (i) of (variable)))
     }
}

That should do it. It breaks up "variable" into the list based on spaces. Let me check that quick...

Edit: Debugged, should work now

Last edited by MoreGamesNow (2011-11-08 19:10:25)


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

Offline

 

#4 2011-11-11 16:18:56

Tylerbreeze
New Scratcher
Registered: 2011-11-09
Posts: 8

Re: strings

Im confused about what variables and what lists to make?

Offline

 

#5 2011-11-11 16:24:00

Lar-Rew
Scratcher
Registered: 2010-02-19
Posts: 100+

Re: strings

You want i and j as variables, variable as a variable that contains the string you want to break, and list as a list.

Last edited by Lar-Rew (2011-11-11 16:24:17)

Offline

 

#6 2011-11-11 16:32:59

Tylerbreeze
New Scratcher
Registered: 2011-11-09
Posts: 8

Re: strings

so like the team names as variable?

Offline

 

#7 2011-11-11 16:55:01

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

Re: strings

variables "i" and "j" are just counters.  You shouldn't be effecting them with other scripts.  The variable called "variable" contains the string you want to split.  The list "list" will contain all of the words in the variable.  In other words, "variable" gets split by spaces, and the parts of variable that result are stored in "list".


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

Offline

 

#8 2011-11-11 16:57:31

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

Re: strings

Code:

set [variable] to ( Layabouts 23 Luddites 14 )
Run Code I Posted Here

The above code results in "list" being this:

1) Layabouts
2) 23
3) Luddites
4) 14


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

Offline

 

#9 2011-11-11 17:17:15

Tylerbreeze
New Scratcher
Registered: 2011-11-09
Posts: 8

Re: strings

then how do i separate this list into one for a win or loss collum? or into the teams?

Offline

 

#10 2011-11-11 17:32:11

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

Re: strings

Compare "item (2) of [list]" and "item (4) of [list] to see who won.  The one with the higher score won the game.


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

Offline

 

Board footer