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

#1 2012-06-28 23:39:20

koolguru
New Scratcher
Registered: 2011-04-13
Posts: 1

A very interesting problem

I have a very interesting problem to solve. How would I find the number of times a certain word appears in a document. (I put the document in as a value for a variable)

Offline

 

#2 2012-06-29 00:03:16

berberberber
Scratcher
Registered: 2012-03-08
Posts: 1000+

Re: A very interesting problem

okay.
You can do it with letters, but not with words.

when gf clicked
set [counter v] to [1]
set [letter count v] to [0]
repeat (length of (document))
 if <(letter (counter) of (document))=[letter wanted]>
  change [letter count v] by (1)
 end
 change [counter v] by (1)
end

Last edited by berberberber (2012-06-29 00:04:27)


http://i47.tinypic.com/2iaa73k.png

Offline

 

#3 2012-06-29 02:05:48

JJROCKER
Scratcher
Registered: 2010-09-06
Posts: 1000+

Re: A very interesting problem

Adding on to what berberberber said... you can just take that script and use multiple letters to read words.

You can also have what is inputted a list or variable, so you can just make a script that reads it.

EDIT: I think my newest project may help you a little bit. I made it so when a certain element is put in, it will tell you a number. You can do something similar with it so instead, it tells you the amount of times it was inputted.

Last edited by JJROCKER (2012-06-29 02:06:37)


http://www.blocks.scratchr.org/API.php?action=random&amp;return=image&amp;link1=http://img255.imageshack.us/img255/3491/signature1y.jpg&amp;link2=http://img577.imageshack.us/img577/5272/signature1sx.jpg&amp;link3=http://img4.imageshack.us/img4/8514/signature1et.jpg&amp;link4=http://i.imgur.com/POEpQyZ.png&amp;link5=http://img163.imageshack.us/img163/4640/jjrockerfinal.jpg

Offline

 

#4 2012-06-29 03:00:48

SciTecCf
Scratcher
Registered: 2011-11-23
Posts: 1000+

Re: A very interesting problem

This is possible, here is a nice code for it:

Put your document into a variable called "document".
Create these variables:
  loopcounter
  adder
and this list:
  words
and use this script:

delete (all v) of [words]
set [adder v] to [] //Blank.
set [loopcounter v] to [1]
repeat (length of (document))
 if <(letter (loopcounter) of (document)) = [ ]> //A space.
  add (adder) to [words v]
  set [adder v] to [] //Blank.
 else
  set [adder v] to (join (adder) (letter (loopcounter) of (document)))
 end
 change [loopcounter v] by (1)
end
add (adder) to [words v]
set [adder v] to [0]
set [loopcounter v] to [1]
repeat (length of [words v])
 if <(item (loopcounter) of [words v]) = [YourWord]>
  change [adder v] by (1)
 end
 change [loopcounter v] by (1)
end
The "adder" variable will contain your answer when the script is finished.

Note: This script can take a while to finish.

Last edited by SciTecCf (2012-06-29 03:05:08)


http://bit.ly/LCZEJRhttp://bit.ly/LSONcOhttp://bit.ly/LF3vIc
http://trinary.site40.net/images/scratchrank.php?username=SciTecCf&amp;display=small

Offline

 

Board footer