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

#1 2009-08-06 11:44:13

Larry828
Scratcher
Registered: 2007-05-30
Posts: 100+

How move to "next" item in a List?

If there's a List of 10 items one can "say" the contents of the list by entering "Say (item 1)" and "Say (item 2)" and "Say (item 3)" etc.  But nothing I've tried allows me to simply "Say" the first item and then have all the others "said" in succession automatically.  Help shows that the contents of the *entire list* can be simultaneously "said" in a balloon. But I would like them individually, in sequential order.  My changes seem to change the *content* of a list item, not move to the *next item*.  Could someone tell me how to code the move from one list item to the next?

Offline

 

#2 2009-08-06 11:58:35

Jens
Scratcher
Registered: 2007-06-04
Posts: 1000+

Re: How move to "next" item in a List?

Hi Larry828,

if you want to iterate over a list you'll need a loop and an index variable.

I usually do something like this:

   set index to 0
   repeat (length of list)
      change index by 1
      say (item index of list) for 1 second
   end repeat

Cheers


Jens Mönig

Offline

 

#3 2009-08-06 12:03:11

The-Whiz
Scratcher
Registered: 2007-07-09
Posts: 1000+

Re: How move to "next" item in a List?

So...

You want it to say
Hello!
wait 2 seconds
Hi!
wait 2 seconds
etc.
.

This can be accomplished with this script:

Code:

/When flag clicked\
[set [NUMBER] to (0)]
[repeat (length of [LIST])]
[][change [NUMBER] by (1)]
[][say (item (NUMBER) of [LIST]) for (2) seconds]
[end of repeat]

I think that will help.

If it doesn't work, I'll try to help you more.

Oops, Jens posted while I was writing this...

Last edited by The-Whiz (2009-08-06 12:03:55)

Offline

 

#4 2009-08-06 19:08:30

Larry828
Scratcher
Registered: 2007-05-30
Posts: 100+

Re: How move to "next" item in a List?

Thank you both, Jens and The-Whiz!  I tried the coding and it worked perfectly. When I finish the project I'm working on, which depends on this script, I'll send each of you a link to it. [What a reward for your assistance!!  :-) ]  _____ I have one further question: it's not obvious to me how, given a List, one can create a Variable (named Index or Number) and have it 'magically' apply to the Item Number in the List. I would have thought that Lists would have a 'property' called Item Number or whatever that would have to be invoked. In fact, I think one of my failed attempts was precisely creating a Variable named "Item Number" but I never could apply it correctly to do what I wanted. (Does this further question make any sense?)  _______ A note to both of you: I noticed our Scratch Registration Dates and we all joined Scratch within a couple of months of each other. (I was the first....and the least knowledgeable!) ________ And a note to The-Whiz: you answered a question I had asked a while ago in this Forum about Scoring. It involved solving the problem that if an arrow goes through an apple it's liable to run up 534 points instead of the "1" that should have been awarded for a Hit. You added the Wait Until Not Touching line before incrementing the score by 1. That was so helpful in so many situations that I made a tutorial project out of it called "Two Scores" in which you were prominently named. So here's yet another Thanks for your help.

Offline

 

#5 2009-08-06 19:20:35

The-Whiz
Scratcher
Registered: 2007-07-09
Posts: 1000+

Re: How move to "next" item in a List?

Hm... I don't understand what you're asking...

Well the (item (index) of [LIST]) does that...

Offline

 

#6 2009-08-06 20:10:19

Jens
Scratcher
Registered: 2007-06-04
Posts: 1000+

Re: How move to "next" item in a List?

Hi again, Larry828.

The magic happens by exchanging the number in the "item_of list" block with a variable. Let me try to explain the idea of an index variable by first going back a couple of steps. You might as well code explicitly:

   say (item '1' of list) for 1 second
   say (item '2' of list) for 1 second
   say (item '3' of list) for 1 second
   say (item '4' of list) for 1 second

... and so on ...

This works fine if your list always stays the same size. As soon as you start adding items to the list, you also have to add additional block to the stack, or else the added items will be left unsaid. Worse: If you remove items from the list, the stack will produce an error, because it will try to report an item which no longer exists.

You can avoid some of this by coding:

   set index to 0
   if (length of list) > index
      change index by 1
      say (item index of list) for 1 second
   end if
   if (length of list) > index
      change index by 1
      say (item index of list) for 1 second
   end if
   if (length of list) > index
      change index by 1
      say (item index of list) for 1 second
   end if
   if (length of list) > index
      change index by 1
      say (item index of list) for 1 second
   end if

... and so on

As you might notice, there is a pattern here which always exactly repeats itself:

   if (length of list) > index
      change index by 1
      say (item index of list) for 1 second
   end if

Such repetitious chunks of code are what loops are made for. In this case the "repeat" loop-block works fine, because you can ask your list how many entries it has, which is precisely how often you want to repeat this little stack of code. Then, of course, there's no need to assert each time that the index variable's value still is within the scope of the list. So you can leave out the "if" block, and condense the code to just:

   set index to 0
   repeat (length of list)
      change index by 1
      say (item index of list) for 1 second
   end repeat

(which is the example)

I hope this helps you get on track with list indices. It's amazing how hard it is to explain these things, once you really think about them.

Within a list each item is stored in a separate slot. In many programming languages such slots are numbered (some starting with zero, others with 1). If you want to access a specific item in the list, you have to know its slot-number. It's like visiting somebody at a new address. You either know their apartment number, or you'll have to go through each floor and ring up everybody in the building until you recognize a face you know  smile


Jens Mönig

Offline

 

#7 2009-08-07 01:52:09

Larry828
Scratcher
Registered: 2007-05-30
Posts: 100+

Re: How move to "next" item in a List?

Thanks Jens for going back to basics. Scratch is my first experience with programming so it's a step-by-step undertaking. With each new project I try to push myself to try something I haven't done before. This time it was trying to get serial access to the contents of the whole list. And I had started by doing exactly what you described: building a sequence of "say" commands, one for each item in the list. It quickly became obvious that such a procedure would be ridiculous for a list of 150 items, so I tried to find another way in. That's when I ran into trouble: if my efforts affected anything at all they seemed to affect the *content* of a list item, not the item's number in the list. So thanks for taking me through the whole process in a logical way. I'll print out your reply for careful study. Thanks to you and The-Whiz for your earlier comments: I was able to finish my project -- allowing someone to play a piano keyboard I built on the screen and my List recorded the notes for replay. And I even made a built-in song that could be activated by clicking the Cat, which meant deleting any current list and substituting another one. But the key to the whole thing was the "serial access" to the list. Thanks again!

Offline

 

Board footer