Getting input from the user – input and ask
The easiest way to get an input is to use the input
word, as shown in the following code snippet:
;-- see Chapter04/getting-input.red:
print "Enter a number: "
num: input
print ["You entered the number" num "with" length? num "digits"]
When interpreted with the console throughred getting-input.red
or from within the console with do %getting-input.red
, we get the following result when the number 89
is entered:
Enter a number: 89 You entered the number 89 with 2 digits
What is the datatype of num
? If we try length? 89
in the console, we get a *** Script Error: length? does not allow integer! for its series
argument. But length? "89"
returns 2
, so num
must be of a string!
type. Indeed, anything typed into the console is received by Red as a string. This is so for theinput
word, and also for the ask
word, which we'll use shortly.
What if we want to compile this little program with red -r
or red -c
? This gives you a *** Compilation Error: undefined word...