Page 1 of 1
Input from Parameter
Posted: Wed Apr 02, 2008 6:19 pm
by scottmaccal
Greetings,
I am trying to come up with a way to get user input in the form of (circle number1 number2) where circle is a defined function and the number1 and number2 are input from the user. I would like number1 and number2 input to go into a (write-line) function that already has characters in it. Here is some of the code I have written thus far.
(define (circle)
(set 'out-file (open "c:/users/name/desktop/circle" "write"))
(write-line "circle number1 number2 " out-file)
(close out-file))
(circle)
Any help would be greatly appreciated.
Posted: Wed Apr 02, 2008 6:45 pm
by cormullion
Hi Scott...
Do you mean this sort of thing:
Code: Select all
(define (circle x y)
(set 'out-file (open "circle" "write"))
(write-line (string "circle " x " " y) out-file)
(close out-file))
> (circle 1 2)
true
> (circle 3 4)
true
> (read-file "circle")
"circle 3 4\n"
>
Only the most recent values were stored. If you want to keep a history, use append-file.
"format" function
Posted: Wed Apr 02, 2008 11:41 pm
by lithper
the standard way to do variable interpolation inside a given string, as far as I understand, is by using the "format" function.
Code: Select all
(write-line (format "blablabla %s blabla %d" string_var decimal_num_var))
Re: "format" function
Posted: Thu Apr 03, 2008 1:07 am
by scottmaccal
lithper wrote:the standard way to do variable interpolation inside a given string, as far as I understand, is by using the "format" function.
Code: Select all
(write-line (format "blablabla %s blabla %d" string_var decimal_num_var))
Thanks for the response cormullion and lithper!
My question now is how do I go about getting input from the user to fill num_var in a defined function?
What would such a function look like?
Posted: Thu Apr 03, 2008 1:26 am
by Lutz
You can use (read-line) to get input from stdin/keyboard. This will work only in a command shell/terminal window, not in the console window of the newLISP-GS Java front-end installed on Windows or Mac OS X.
Posted: Thu Apr 03, 2008 11:53 pm
by scottmaccal
Lutz wrote:You can use (read-line) to get input from stdin/keyboard. This will work only in a command shell/terminal window, not in the console window of the newLISP-GS Java front-end installed on Windows or Mac OS X.
Thank you.
That gets me heading in the right direction.
Posted: Fri Apr 04, 2008 12:17 am
by Jeff
You will also want to cast them to integers or floats using (int foo) or (float foo).
Posted: Fri Apr 04, 2008 1:49 pm
by scottmaccal
Jeff wrote:You will also want to cast them to integers or floats using (int foo) or (float foo).
How would I go about handling things that are not digits?
My latest challenge is writing code that will call a defined function after the user has inputed a certain sting. This is the code I have so far:
Code: Select all
(print "What shape: ")
(set 'answer (read-line))
(if
(answer "draw-circle")
(circle))
Could you point me in the right direction?
Posted: Fri Apr 04, 2008 2:39 pm
by Jeff
Code: Select all
(print "What shape: ")
(set 'answer (read-line))
(if (= answer "draw-circle")
(circle))
That would make *that* work properly. What I was talking about was that read-line interns strings. After (set 'answer (read-line)), answer now contains a string. If you were accepting, for example, a radius for a circle that would get used as a float, you would do:
Code: Select all
(print "What radius: ")
(set 'answer (read-line))
(if (set 'answer (float answer))
(do-something-with answer)
(begin
(print "What *float* radius: ")
(set 'answer (read-line))))
Posted: Fri Apr 04, 2008 5:04 pm
by cormullion
Just for fun, here's a gui version of a circle-drawing routine. I was reminded by your posts of the fun we had typing responses to commands, and how complicated the business of getting the right information from a user on the command line could be (decimal commas, exponents, ranges and limits, etc.)
Code: Select all
(load (append (env "NEWLISPDIR") "/guiserver.lsp"))
(gs:init)
(gs:frame 'f 50 50 550 600) ; a window
(gs:set-flow-layout 'f) ; layout type
(gs:canvas 'a-canvas) ; a canvas
(gs:set-background 'a-canvas gs:white)
(gs:set-size 'a-canvas 500 500)
(gs:slider 'radius-slider 'radius-slider-handler "horizontal" 0 800 100) ; a slider
(gs:label 'radius-label {radius}) ; a label
(define (radius-slider-handler id value) ; handler
(cond
((= id "MAIN:radius-slider")
(set 'scale (div (float value) 100))
(gs:set-scale scale scale)
(gs:set-text 'radius-label (string (int (div value 2)) { pixels}))
(gs:update))))
; assemble
(gs:add-to 'f 'a-canvas 'radius-slider 'radius-label)
(gs:set-translation 250 250)
(set 'circle-radius 30)
(gs:draw-circle 'circle 0 0 circle-radius)
(gs:set-visible 'f true)
; run
(gs:listen)
Posted: Sat Apr 05, 2008 12:18 am
by scottmaccal
Jeff,
Thank you! That info was a big help.
Cormullion,
Thanks for sharing the GUI code. Even though it looks quite complex, it is remarkable to me how readable it is.