In lieu of two dimentional lists...
I want a reporter block that will search a list for a specified string and return the index of the first match.
Something like:
( index of [____________] in [My List |v] )
It could return 0 if there is no match.
This could probably be based on the existing code for the < contains > block. Except it would return the index instead of true/false.
It could be called "index of" or "position of" or "location of" or "item number of".
EDIT:
By the way, I'm not looking for a way to do it with basic blocks. I know how to do it. The reason I'm looking for a new block is that native blocks run MUCH faster.
Last edited by BoltBait (2011-08-01 20:40:41)
Offline
BoltBait wrote:
I want a reporter block that will search a list for a specified string and return the index of the first match.
Something like:
< index of [____________] in [My List |v] >
It could return 0 if there is no match.
Well technically, you made it look like a boolean even though it would be a reporter.
But yeah, we could this. The workaround is kinda painful, especially for beginners.

Offline
Ooh, I agree! I've always wanted a block like this. In addition, I think I'd be nice if you could choose which of the entries it would choose if there were multiple ones, like the first or last instance, or any number.
Offline
This is a really good idea. When it comes to projects that are very list-heavy -- for example CatBOT 2.0 (a chat bot project Adriangl and I collaborated on), this would come in handy very much. As you mentioned, there are ways to do it with a series of blocks, but when it comes to lists of more than a hundred values, saying that "this is slow" would be an understatement. I completely support your suggestion. However for new scratchers, the word "index" might not immediately signal the "first value of this in a list" -- I think using some simpler words would make it a bit better:
( first time [____________] appears in [My List |v] )
It's a bit more lengthy which is never completely favorable, but its a bit more comprehensive. Also, perhaps this block?
( number of times [____________] appears in [My List |v] )
What do you think?
Last edited by Lucario621 (2011-08-01 20:42:23)
Offline
Lucario621 wrote:
This is a really good idea. When it comes to projects that are very list-heavy -- for example CatBOT 2.0 (a chat bot project Adriangl and I collaborated on), this would come in handy very much. As you mentioned, there are ways to do it with a series of blocks, but when it comes to lists of more than a hundred values, saying that "this is slow" would be an understatement. I completely support your suggestion. However for new scratchers, the word "index" might not immediately signal the "first value of this in a list" -- I think using some simpler words would make it a bit better:
( first time [____________] appears in [My List |v] )
It's a bit more lengthy which is never completely favorable, but its a bit more comprehensive. Also, perhaps this block?
( number of times [____________] appears in [My List |v] )
What do you think?
I think that you're correct in both ways
And I like that second block as well.
And I support, of course, the idea's awesome. On some test projects I've been doing I've been having to do this, and it's really a hassle. And I used some long lists as well
The only problem is that all of these suggestions will probably suffocate the Scratch Team, I mean, there are a ton, and even if it's minorly helpful many people support it. I think we should only support some really good ones and maybe some regular suggestions that would be helpful, in order not to make the scratch team overwork.

Offline
BoltBait: I made the Squeak code for your block! Now I can use this block whenever I want

Offline
Greenatic wrote:
BoltBait: I made the Squeak code for your block! Now I can use this block whenever I want
![]()
And I just made the other one too...

Offline
Greenatic wrote:
BoltBait: I made the Squeak code for your block! Now I can use this block whenever I want
![]()
Fat lot of good that does me!
I don't use mods.
Oh well... hopefully one day I'll have it.
So, post up the squeek code so that other people can use it.
Last edited by BoltBait (2011-08-01 22:17:47)
Offline
I can do this in BYOB but having it in scratch will be EPIC!!!
Offline
OK, here is the code for the above blocks, for use by Scratch modifiers. Please give credit in any program or project where you use these blocks or blocks based on their code. Thanks

('first index of %s in %L' #r #IndexOf:List: '' 'list')Method code:
IndexOf: t1 List: t2
| t3 |
t3 _ 1.
(self lineCountOfList: t2)
timesRepeat:
[(self getLine: t3 ofList: t2)
= t1 ifTrue: [^ t3].
t3 _ t3 + 1].
^ 0Scratch-Objects > ScriptableScratchMorph > blocks > defaultArgsFor:
Add right before the last line ( ^ t2 ):
#IndexOf:List: = t4
ifTrue: [t2 size >= 2
ifTrue:
[t2 at: 1 put: (t2 at: 1) localized.
t2 at: 2 put: self defaultListName]].
('times %s appears in %L' #r #Times:List: '' 'list')Method code:
Times: t1 List: t2
| t3 t4 |
t3 _ 1.
t4 _ 0.
(self lineCountOfList: t2)
timesRepeat:
[(self getLine: t3 ofList: t2)
= t1 ifTrue: [t4 _ t4 + 1].
t3 _ t3 + 1].
^ t4Scratch-Objects > ScriptableScratchMorph > blocks > defaultArgsFor:
Add right before the last line ( ^ t2 ):
#Times:List: = t4
ifTrue: [t2 size >= 2
ifTrue:
[t2 at: 1 put: (t2 at: 1) localized.
t2 at: 2 put: self defaultListName]].
Offline
I would love that block i used the same idea in one of my projects but yeah i would die to have it. Greenatic the block library is cool but you can't upload a project with a custom block in it to scratch.
Offline
(position of ()th time [] is in [v])
Offline