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

#1 2012-02-27 22:58:27

ProgramCAT
Scratcher
Registered: 2011-12-13
Posts: 500+

Help with finding substrings

I need help finding substrings within an input...
In my project, I have a very long string and a list of substrings I want to find (they may or may not be present, though).
After finding these substrings, the script should then report back the positions and length of the substrings found.

Any ideas?  hmm


Programming is an art...
Goodbye, Scratch. I am leaving because of the exams coming up at our school, though I'll check the forums once or twice a week.

Offline

 

#2 2012-02-28 00:30:35

ProgramCAT
Scratcher
Registered: 2011-12-13
Posts: 500+

Re: Help with finding substrings

Anyone?  sad


Programming is an art...
Goodbye, Scratch. I am leaving because of the exams coming up at our school, though I'll check the forums once or twice a week.

Offline

 

#3 2012-02-28 05:16:06

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

Re: Help with finding substrings

Well, if it's a single character, I'd recommend looping through, checking for it. Otherwise... try creating substrings of the lengths your checking, starting at the current index. Check if any of those are what you want. Kind of slow and painful, I know. I wish Scratch had native indexOf(). ^^

Offline

 

#4 2012-02-28 10:31:22

Smozzick
Scratcher
Registered: 2011-10-23
Posts: 100+

Re: Help with finding substrings

ok, list of substrings, presumably held in a real list. And the string presumably held in a variable.

So something like:

set (SubStringCounter) to [0]
Repeat <Length of (ListOfSubStrings)>
change (SubStringCounter) by [1]
repeat until <(counter) = <<Length of (StringHolder)> - [5]>>

if <<letter [1] of <item (SubStringCounter) of [ListOfSubStrings v]>> = <letter [1] of (StringHolder)>>
set (LetterCounter) to [0]
set (MatchCounter) to [0]

Repeat <length of <item (SubStringCounter) of [ListOfSubStrings v]>>
change (LetterCounter) by [1]

if <<letter (LetterCounter) of <item (SubStringCounter) of [ListOfSubStrings v]>> = <letter <(counter) + (LetterCounter)> of (SubStringCounter)>>
change (MatchCounter) by [1]
else
set (MatchCounter) to [0]
end

end
if <(MatchCounter) = <length of <item (SubStringCounter) of [ListOfSubStrings v]>>>
stuff happens
end
end

end

end

----

Basically, there's no simple way to do it but something like the above would work...you'd need another counter to be able to store positions, though. This is the incredible number of counters method  wink


http://i50.tinypic.com/ded8m.png

Offline

 

#5 2012-02-28 13:17:58

ProgramCAT
Scratcher
Registered: 2011-12-13
Posts: 500+

Re: Help with finding substrings

Thanks!  big_smile


Programming is an art...
Goodbye, Scratch. I am leaving because of the exams coming up at our school, though I'll check the forums once or twice a week.

Offline

 

#6 2012-02-28 13:57:54

Smozzick
Scratcher
Registered: 2011-10-23
Posts: 100+

Re: Help with finding substrings

ProgramCAT wrote:

Thanks!  big_smile

No problem, tell me if you have any trouble with it - I haven't had a chance to test it out but it should work.


http://i50.tinypic.com/ded8m.png

Offline

 

#7 2012-03-02 04:56:33

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Help with finding substrings

Closed by request of the topic owner.


http://i39.tinypic.com/2nav6o7.gif

Offline

 

Board footer