This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.
  • Index
  •  » Suggestions
  •  » Block to FIND something in a LIST returning first INDEX of match

#1 2011-08-01 16:51:33

BoltBait
Scratcher
Registered: 2009-03-09
Posts: 1000+

Block to FIND something in a LIST returning first INDEX of match

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)


Animated sigs must be banned!
http://boltbait.com/j.pnghttp://boltbait.com/s.pnghttp://boltbait.com/d.pnghttp://boltbait.com/a.pnghttp://boltbait.com/p.png

Offline

 

#2 2011-08-01 17:03:49

Greenatic
Scratcher
Registered: 2009-05-03
Posts: 1000+

Re: Block to FIND something in a LIST returning first INDEX of match

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

 

#3 2011-08-01 17:05:15

BoltBait
Scratcher
Registered: 2009-03-09
Posts: 1000+

Re: Block to FIND something in a LIST returning first INDEX of match

Ack!  You're right.  I have replaced the "<" and ">" with "(" and ")".  That should do it.


Animated sigs must be banned!
http://boltbait.com/j.pnghttp://boltbait.com/s.pnghttp://boltbait.com/d.pnghttp://boltbait.com/a.pnghttp://boltbait.com/p.png

Offline

 

#4 2011-08-01 19:29:06

Harakou
Community Moderator
Registered: 2009-10-11
Posts: 1000+

Re: Block to FIND something in a LIST returning first INDEX of match

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.


http://www.blocks.scratchr.org/API.php?action=random&amp;return=image&amp;link1=http://i.imgur.com/OZn2RD3.png&amp;link2=http://i.imgur.com/duzaGTB.png&amp;link3=http://i.imgur.com/CrDGvvZ.png&amp;link4=http://i.imgur.com/POEpQyZ.png&amp;link5=http://i.imgur.com/ZKJF8ac.png

Offline

 

#5 2011-08-01 20:41:20

Lucario621
Community Moderator
Registered: 2007-10-03
Posts: 1000+

Re: Block to FIND something in a LIST returning first INDEX of match

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)


http://i.imgur.com/WBkM2QQ.png

Offline

 

#6 2011-08-01 20:53:02

Death_Wish
Scratcher
Registered: 2011-07-26
Posts: 1000+

Re: Block to FIND something in a LIST returning first INDEX of match

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  tongue  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  hmm
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.


With a straight flush.
http://4.bp.blogspot.com/-FlUhnzmIROE/TgtpjdJl4tI/AAAAAAAABCM/W19pvFTZFaU/s1600/save_the_world.png http://blocsonic.com/images/special/rip-stevejobs.jpg

Offline

 

#7 2011-08-01 21:07:09

Greenatic
Scratcher
Registered: 2009-05-03
Posts: 1000+

Re: Block to FIND something in a LIST returning first INDEX of match

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

Offline

 

#8 2011-08-01 21:14:09

Greenatic
Scratcher
Registered: 2009-05-03
Posts: 1000+

Re: Block to FIND something in a LIST returning first INDEX of match

Greenatic wrote:

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

And I just made the other one too...

Offline

 

#9 2011-08-01 22:17:06

BoltBait
Scratcher
Registered: 2009-03-09
Posts: 1000+

Re: Block to FIND something in a LIST returning first INDEX of match

Greenatic wrote:

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

Fat lot of good that does me!  mad

I don't use mods.  sad

Oh well... hopefully one day I'll have it.  smile

So, post up the squeek code so that other people can use it.

Last edited by BoltBait (2011-08-01 22:17:47)


Animated sigs must be banned!
http://boltbait.com/j.pnghttp://boltbait.com/s.pnghttp://boltbait.com/d.pnghttp://boltbait.com/a.pnghttp://boltbait.com/p.png

Offline

 

#10 2011-08-01 22:21:14

coolstuff
Community Moderator
Registered: 2008-03-06
Posts: 1000+

Re: Block to FIND something in a LIST returning first INDEX of match

I rather agree with your suggestion! It'd be really handy, and, yes, this block would run *much* faster than the current way of doing it. I support.  smile

Offline

 

#11 2011-08-02 15:28:21

joefarebrother
Scratcher
Registered: 2011-04-08
Posts: 1000+

Re: Block to FIND something in a LIST returning first INDEX of match

I can do this in BYOB but having it in scratch will be EPIC!!!


My latest project is called http://tinyurl.com/d2m8hne! It has http://tinyurl.com/d395ygk views, http://tinyurl.com/cnasmt7 love-its, and http://tinyurl.com/bwjy8xs comments.
http://tinyurl.com/756anbk   http://tinyurl.com/iplaychess

Offline

 

#12 2011-08-02 17:33:40

Greenatic
Scratcher
Registered: 2009-05-03
Posts: 1000+

Re: Block to FIND something in a LIST returning first INDEX of match

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  smile

http://polyeztahpuppies.webs.com/firstindex.gif

Code:

('first index of %s in %L' #r #IndexOf:List: '' 'list')

Method code:

Code:

IndexOf: t1 List: t2 
    | t3 |
    t3 _ 1.
    (self lineCountOfList: t2)
        timesRepeat: 
            [(self getLine: t3 ofList: t2)
                = t1 ifTrue: [^ t3].
            t3 _ t3 + 1].
    ^ 0

Scratch-Objects > ScriptableScratchMorph > blocks > defaultArgsFor:
Add right before the last line ( ^ t2 ):

Code:

#IndexOf:List: = t4
        ifTrue: [t2 size >= 2
                ifTrue: 
                    [t2 at: 1 put: (t2 at: 1) localized.
                    t2 at: 2 put: self defaultListName]].

http://polyeztahpuppies.webs.com/timesappearsinlist.gif

Code:

('times %s appears in %L' #r #Times:List: '' 'list')

Method code:

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].
    ^ t4

Scratch-Objects > ScriptableScratchMorph > blocks > defaultArgsFor:
Add right before the last line ( ^ t2 ):

Code:

#Times:List: = t4
        ifTrue: [t2 size >= 2
                ifTrue: 
                    [t2 at: 1 put: (t2 at: 1) localized.
                    t2 at: 2 put: self defaultListName]].

Offline

 

#13 2011-08-08 11:12:56

slinger
Scratcher
Registered: 2011-06-21
Posts: 1000+

Re: Block to FIND something in a LIST returning first INDEX of match

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.


http://s0.bcbits.com/img/buttons/bandcamp_130x27_blue.png

Offline

 

#14 2011-08-08 11:18:18

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: Block to FIND something in a LIST returning first INDEX of match

(position of ()th time [] is in [v])

Offline

 
  • Index
  •  » Suggestions
  •  » Block to FIND something in a LIST returning first INDEX of match

Board footer