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

#1 2010-01-19 16:47:38

dkrrrunner
Scratcher
Registered: 2009-09-24
Posts: 7

Creating a substring function (Message Not Understood)

Some work has been done to create a substring function that takes a string and two numbers for the range.  In Scratch 1.3, the function worked perfectly and variables could be used for the numbers in the range. However, when moving it to Scratch 1.4, I cannot use a variable instead of a number for the lower part of the range. Debugging it, it gives me a message not understood. Here's a sample program that would worked in Scratch 1.3, but not in Scratch 1.4

set i = 1
set j = 4
set str = "HelloWorld"
substring(str) in range (i)(j) \\ERROR

The function works fine if two numbers are entered for the range or even if a number is just entered for the first number. It does not work when the first number of the range becomes a variable, even if that variable is a number. Does anyone have any ideas? Thanks so much.

Offline

 

#2 2010-01-24 13:51:19

billyedward
Scratcher
Registered: 2008-01-03
Posts: 500+

Re: Creating a substring function (Message Not Understood)

Are you looking to create a function in scratch or squeak?
For scratch, you'll want to loop through the number of characters needed, and use the characterAt:in: block to add each one to a list. Then, the list reporter block gives the concatenation of those letters.

In squeak, it is 5 times simpler:
('substring between %n and %n of %s' #r #subStrOf:from:to:) in class.
And now, make a function under other specs that reads:
subStrOf: t1 from: t2 to: t3
    ^ t3 copyFrom: (t1 min: t2)
        to: (t1 max: t2)


"I'd love to change the world, but they haven't released the source code yet."
Check out the latest version of Streak --> http://billy.scienceontheweb.net/Streak

Offline

 

Board footer