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

#1 2012-02-19 20:01:37

360-International
Scratcher
Registered: 2011-07-17
Posts: 100+

Tutorial: Testing if something is a string or a number. (12-Feb-19)

Older tutorials: Wall Jumping (12-Feb-01)

This is a quick script to test whether or not a variable's value is a string (text) or a number.

NOTE: This is not the same as checking if something is a word or not.

Make this variable:

(report)
Then add this script where you need to check the input:

if <(input) = ((1) * (input))> //Replace "input" with the actual input.
set [report v] to [number]
else
set [report v] to [string]
end
The variable "report" will say "number" if it is a number, and "string" if it is not.

How this works:
If Scratch encounters a string that it has to operate with, it evaluates the variable as equaling zero, whereas if not, the variable is evaluated as the string it contains. In other words, "input" alone is what is in the variable. However, when anything is done to it (for example, multiplying it by 1), the string must be turned into a number. Scratch will always turn any string into the number 0 when it needs to. (This does not change the value of the variable.)

If you find any way to improve on the scripts above, please reply.

Last edited by 360-International (2012-02-19 20:11:31)


https://lh4.googleusercontent.com/Oib6kyze-OrCFIRlqL6HYnWuHBQp1GfFoh5CkBlZGe3_9WCk-_DDwgfFrnD_mxJ_x2b18hMUyV2KVtIDiLdebyWaQQ6fbpKpZs-8auVep1zoSgOv-iA

Offline

 

#2 2012-02-19 20:04:16

laptop97
Scratcher
Registered: 2011-06-27
Posts: 1000+

Re: Tutorial: Testing if something is a string or a number. (12-Feb-19)

Cool

Offline

 

#3 2012-02-19 20:31:49

AtomicBawm3
Scratcher
Registered: 2009-06-27
Posts: 1000+

Re: Tutorial: Testing if something is a string or a number. (12-Feb-19)

Very clever...that'll be helpful for games and such that use the ask block.


http://i50.tinypic.com/j0yw0p.jpg

Offline

 

#4 2012-02-20 03:40:37

analytic
New Scratcher
Registered: 2012-02-20
Posts: 90

Re: Tutorial: Testing if something is a string or a number. (12-Feb-19)

This technique has existed for some time.
But it certainly will be very useful in many ask/answer projects in the future.

Offline

 

#5 2012-02-20 06:16:40

bobbybee
Scratcher
Registered: 2009-10-18
Posts: 1000+

Re: Tutorial: Testing if something is a string or a number. (12-Feb-19)

if <<(input) = ((1) * (input))> or <(input) = [0]>> //Replace "input" with the actual input.
set [report v] to [number]
else
set [report v] to [string]
end

Last edited by bobbybee (2012-02-20 06:17:05)


I support the Free Software Foundation. Protect our digital rights!

Offline

 

#6 2012-02-20 20:33:32

360-International
Scratcher
Registered: 2011-07-17
Posts: 100+

Re: Tutorial: Testing if something is a string or a number. (12-Feb-19)

bobbybee wrote:

if <<(input) = ((1) * (input))> or <(input) = [0]>> //Replace "input" with the actual input.
set [report v] to [number]
else
set [report v] to [string]
end

How does this help? (not criticizing)


https://lh4.googleusercontent.com/Oib6kyze-OrCFIRlqL6HYnWuHBQp1GfFoh5CkBlZGe3_9WCk-_DDwgfFrnD_mxJ_x2b18hMUyV2KVtIDiLdebyWaQQ6fbpKpZs-8auVep1zoSgOv-iA

Offline

 

#7 2012-02-21 03:10:41

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

Re: Tutorial: Testing if something is a string or a number. (12-Feb-19)

360-International wrote:

bobbybee wrote:

if <<(input) = ((1) * (input))> or <(input) = [0]>> //Replace "input" with the actual input.
set [report v] to [number]
else
set [report v] to [string]
end

How does this help? (not criticizing)

It's an improved version. Your version will report "string" if it's a number, but this doesn't.

Offline

 

#8 2012-02-21 10:14:02

rdococ
Scratcher
Registered: 2009-10-11
Posts: 1000+

Re: Tutorial: Testing if something is a string or a number. (12-Feb-19)

scimonster wrote:

360-International wrote:

bobbybee wrote:

if <<(input) = ((1) * (input))> or <(input) = [0]>> //Replace "input" with the actual input.
set [report v] to [number]
else
set [report v] to [string]
end

How does this help? (not criticizing)

It's an improved version. Your version will report "string" if it's a number, but this doesn't.

Actually, if the input was 0,

<(input) = ((1) * (input))>
equals true, which will report "number". And 0 IS a number.

Offline

 

#9 2012-02-21 10:32:01

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: Tutorial: Testing if something is a string or a number. (12-Feb-19)

An example of what you could use it for:

when gf clicked
forever
    ask [Enter a number or string] and wait
    if <(answer) = <(1) * (answer)>>
        say [Number] for (2) secs
    else
        say [String] for (2) secs
    end
And yes, inputting a zero will work.  smile

Offline

 

#10 2012-02-21 10:39:20

RedRocker227
Scratcher
Registered: 2011-10-26
Posts: 1000+

Re: Tutorial: Testing if something is a string or a number. (12-Feb-19)

Cool.


Why

Offline

 

#11 2012-02-21 11:22:01

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

Re: Tutorial: Testing if something is a string or a number. (12-Feb-19)

rdococ wrote:

scimonster wrote:

360-International wrote:

How does this help? (not criticizing)

It's an improved version. Your version will report "string" if it's a number, but this doesn't.

Actually, if the input was 0,

<(input) = ((1) * (input))>
equals true, which will report "number". And 0 IS a number.

Oh, you're right.  tongue  I didn't realize that. XD

Offline

 

#12 2012-02-21 14:30:28

bobbybee
Scratcher
Registered: 2009-10-18
Posts: 1000+

Re: Tutorial: Testing if something is a string or a number. (12-Feb-19)

Doh!


I support the Free Software Foundation. Protect our digital rights!

Offline

 

#13 2012-03-11 22:33:25

360-International
Scratcher
Registered: 2011-07-17
Posts: 100+

Re: Tutorial: Testing if something is a string or a number. (12-Feb-19)

bobbybee wrote:

Doh!

Huh?


https://lh4.googleusercontent.com/Oib6kyze-OrCFIRlqL6HYnWuHBQp1GfFoh5CkBlZGe3_9WCk-_DDwgfFrnD_mxJ_x2b18hMUyV2KVtIDiLdebyWaQQ6fbpKpZs-8auVep1zoSgOv-iA

Offline

 

Board footer