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?
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 ); 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: … ?
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 , and the next step will be executed.
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!
else
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
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
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
__else
____play note 72 for 1 beats
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
But now let’s see what happens when you put a global variable in
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: