For each and visual stepping

I want a list of names to check if a particular name is included. If this is the case, then it should be output that the name was found. Otherwise the list should be searched further.

If I now use visual stepping, the process always stops after the first name. What is the reason for this?

Edit: can't include an image of my code.


Hello snapokrator, welcome to this forum!

As a forum newbie you can’t immediately upload your code, you’ll have to wait a few hours or days, or perhaps it depends on how many posts you’ve read.

grumpy comment

I wonder if this policy is really useful, but that’s how it is for now.

Once you’re enabled, right-click (on a tablet: click-and-hold) on any part if your code, and a menu will pop up; select “script pic” (download :white_check_mark:); switch to the forum post you’re writing; select the “picture” icon; select your (probably) most recent file, which will then be uploaded to your post.

In the meanwhile, could you describe your code in words?
Your post’s title suggests you have been using FOR EACH (item) IN (data) (action) - is that correct?
If so, does your code resemble: … ?

For each script pic

For each script pic 2

I found nothing strange while running the above code in step-mode. Yes it stopped after every step, but isn’t that the idea of a step-mode? You press :play_or_pause_button:, and the next step will be executed.

By the way you could also use:

untitled script pic 137

Or does it work like this block?
untitled script pic 82
It’s kinda hard to tell based on the op
(Also that script needs to warped)

thank you for this detailed example. I always have the problem that when I run (visual steps) not all names are run. The process always ends after the first entry in the list.

My code:

set found to false

set nameslist to list [moritz][josef][max][markus]

for each name in nameslist
if name = max
set found to true
report max found!
report search ongoing!

Because it’s reporting on the first list item always due to the else,are you sure that only this is what’s messing it up from visual stepping, or is there another script that this one depends on

If you know how try using the [scratchblocks] tag to represent it as well

Is “name list” a script variable or a block variable also

Thanks for your reply!

Does that mean that the report block terminates the for each loop? How do you implement it if you want the following output (until max is found):

first item moritz != max
Output: search ongoing

second item josef != max
Output search ongoing

third item max = max (true)
Outpot max found!

The report block immediately terminates blocks, blocks can’t report more than 1 thing per call

You also don’t need the found variable unless it’s used externally
untitled script pic 86

Is this the script you made?

This immediately terminates the loop because report stops blocks (hence the cap)
Depending on what you want the block to output I could help with that
Edit: wrong variable in one of the blocks

looks pretty similar, instead of else if I just used else.
My expectation was that the script says "not found" for each "wrong" item in the list until it finds the right one. With this script it works with a "beep" instead of "not found" as output.

set nameslist to list moritz josef max markus

for each name in nameslist
__if name = max
____report found
____play note 72 for 1 beats

So something like this?

Or like this?

If you don’t understand why this is like this I’d be happy to make an Easier to understand version of this

I know this is kind of off topic, but if you are a beginner working with lists I want to warn you about list linking
In this script pay attention to the variable names and what is reported

This happens because when the “b” variable gets set to “a” it’s not setting the value of every list item to a new list, it’s setting a reference to the “a” list
So when the last item in a is deleted the last item in b also gets deleted

This also applies to custom blocks and script variables, this block removes every other item in a list
untitled script pic 90

But now let’s see what happens when you put a global variable in

The global variable gets modified outside the custom block!
To prevent a list from becoming linked when set use this

untitled script pic 92
And keep in mind these blocks

Keep their input list linked with the ouptut list

This seems to confuse most beginners so just wanted to make you awa

Thanks for your great explanation! Very helpful!
Another question: is there a way to use an array instead of a list?

List is just another word for array

Well, more or less. A list is a very flexible data structure, and in Snap! it doubles as an array.

Python also uses lists, and no arrays. Javascript uses arrays, but they function very similar to pythons lists. Pretty sure most programming languages are like this.

Edit: I did a bit of research and apparently they are fundamentally the same, but have different behaviors. Personally I wouldn't care about having a difference in snap, as there's not really a reason to have 2 differently functioning lists.

This is not a good explanation of how different they are, because frankly, I don't care, especially if we're talking about it snap.

True. Other languages too, such as Lisp and Scheme.

For reference, Wikipedia’s definition of an array is: “a data structure consisting of a collection of elements or variables of same memory size, each identified by at least one array index or key. An array is stored such that the position of each element can be computed from its index tuple by a mathematical formula.” - I think this is very much a 3rd generation programming language concept, and much stricter than Snap!’s list, JavaScript’s array, etc.

“Under the hood” a Snap! list is either implemented as a linked list, or an array, or a combination thereof. From a user perspective the only consequence is some operations being executed faster or slower.

We tried hard to make every widely used programming style efficient. But it turns out that your data set has to be very big before the algorithmic issues outweigh the speed of using JavaScript's native data structure.

What you are really warning against is imperative programming. The right way to handle lists (or any data aggregate) is functional programming, in which you don't change the value of a variable, but instead make a new list:

No looping, no index variables, just the function that you want to call for each item in the list.

In your example,

Learn to think "how can I do this all at once?" rather than "let me make a loop..."

P.S. There's a primitive CONTAINS block that does this, so you don't have to write even this one-line procedure. :slight_smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.