[REBOL] Re: need help with comparison operator <> evaluation
From: greggirwin:mindspring at: 14-Nov-2003 11:39
Hi Steve,
Arie nailed your problem, so I'll just add a couple notes if I may.
SV> INPUT-FILE: ask "Enter name of file with numbers to be processed: " comment { user
enters owwnumbers.txt }
SV> OUTPUT-FILE: ask "Enter name of output file: "
SV> INPUT-FILE: make string! INPUT-FILE
SV> OUTPUT-FILE: make string! OUTPUT-FILE
ASK returns a string, so the "make string!" lines don't do anything
here; you can remove them.
SV> comment { Are the last 4 chars of the str equal to ".txt"? If not append ".txt" to
the end of the string. }
SV> EXTENSION-IN: substr INPUT-FILE ( (length? INPUT-FILE ) - 3) 4
SV> print EXTENSION-IN comment { prints out .txt on the screen}
SV> EXTENSION-OUT: substr OUTPUT-FILE ( (length? OUTPUT-FILE ) - 3) 4
Newer versions of REBOL have a SUFFIX? function you might find
helpful in the future.
SV> if [EXTENSION-IN <> ".txt"] [append INPUT-FILE ".txt" ]
SV> if [EXTENSION-OUT <> ".txt"] [append OUTPUT-FILE ".txt" ]
SV> INPUT-FILE: make file! INPUT-FILE
SV> OUTPUT-FILE: make file! OUTPUT-FILE
The choice between MAKE and TO isn't always clear, but most folks use
TO more--probably because it's shorter. :) The main thing for me is
what the words convey when you read them. i.e. are you telling the
reader that you're making a new value or converting one?
Now, the next step would be to generalize this stuff in case you need
it again in the future:
change-suffix: func [
{Changes the suffix of the string and returns the updated string.}
string [any-string!] "The file, url, string, etc. to change."
suffix [any-string!] "The new suffix."
/local s
][
attempt [if #"." <> first suffix [suffix: join %. suffix]]
append either s: find string suffix? string [clear s][string] suffix
]
; Maybe use refinements for default and suffix rather than
; requiring them.
ask-for-file: func [prompt default suffix /local answer result] [
change-suffix to file!
either empty? answer: ask prompt [default][answer] suffix
]
>> ask-for-file "Give me a file name: " %in-file %.txt
Give me a file name: test
== %test.txt
>> ask-for-file "Give me a file name: " %in-file %.txt
Give me a file name:
== %in-file.txt
If you're new to REBOL, don't let the above code scare you off. I know
it would have looked totally foreign to me when I started out with
REBOL. :)
--Gregg