Pages: 1
Topic closed
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
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)
Offline
Topic closed
Pages: 1