need help with comparison operator <> evaluation
[1/10] from: sendit2me:linuxmail at: 15-Nov-2003 0:06
Greetings,
It seems like I always have the hardest time trying to do the simplest things. For
instance in the code snipet below the not equal comparison EXTENSION-IN <> ".txt" always
evaluates to true and always appends ".txt" to the end of the string even if there's
already .txt at the end of the string. I don't understand. Thanks in advance.
INPUT-FILE: ask "Enter name of file with numbers to be processed: " comment { user enters
owwnumbers.txt }
OUTPUT-FILE: ask "Enter name of output file: "
INPUT-FILE: make string! INPUT-FILE
OUTPUT-FILE: make string! OUTPUT-FILE
comment { Are the last 4 chars of the str equal to ".txt"? If not append ".txt" to the
end of the string. }
EXTENSION-IN: substr INPUT-FILE ( (length? INPUT-FILE ) - 3) 4
print EXTENSION-IN comment { prints out .txt on the screen}
EXTENSION-OUT: substr OUTPUT-FILE ( (length? OUTPUT-FILE ) - 3) 4
if [EXTENSION-IN <> ".txt"] [append INPUT-FILE ".txt" ]
if [EXTENSION-OUT <> ".txt"] [append OUTPUT-FILE ".txt" ]
INPUT-FILE: make file! INPUT-FILE
OUTPUT-FILE: make file! OUTPUT-FILE
DIAL-PEERS-NUMBERS: read/lines INPUT-FILE ...
** Access Error: Cannot open /C/Documents and Settings/svondrasek/Desktop/REBOL/owwnumbers.txt.txt
** Near: DIAL-PEERS-NUMBERS: read/lines INPUT-FILE
Steve Vondrasek
--
______________________________________________
Check out the latest SMS services @ http://www.linuxmail.org
This allows you to send and receive SMS through your mailbox.
Powered by Outblaze
[2/10] from: apwing:zonnet:nl at: 14-Nov-2003 17:37
Hi Steve,
AFAIK you need to remove the [ ] around the condition in the IF statement.
Have a look at http://www.rebol.com/docs/words/wif.html
Met vriendelijke groet / with kind regards,
Arie van Wingerden
http://home.zonnet.nl/rebolution
ICQ 343101686
----- Original Message -----
From: Steve Vondrasek
To: [rebol-list--rebol--com]
Sent: Friday, November 14, 2003 5:06 PM
Subject: [REBOL] need help with comparison operator <> evaluation
Greetings,
It seems like I always have the hardest time trying to do the simplest things. For
instance in the code snipet below the not equal comparison EXTENSION-IN <> ".txt" always
evaluates to true and always appends ".txt" to the end of the string even if there's
already .txt at the end of the string. I don't understand. Thanks in advance.
INPUT-FILE: ask "Enter name of file with numbers to be processed: " comment { user enters
owwnumbers.txt }
OUTPUT-FILE: ask "Enter name of output file: "
INPUT-FILE: make string! INPUT-FILE
OUTPUT-FILE: make string! OUTPUT-FILE
comment { Are the last 4 chars of the str equal to ".txt"? If not append ".txt" to the
end of the string. }
EXTENSION-IN: substr INPUT-FILE ( (length? INPUT-FILE ) - 3) 4
print EXTENSION-IN comment { prints out .txt on the screen}
EXTENSION-OUT: substr OUTPUT-FILE ( (length? OUTPUT-FILE ) - 3) 4
if [EXTENSION-IN <> ".txt"] [append INPUT-FILE ".txt" ]
if [EXTENSION-OUT <> ".txt"] [append OUTPUT-FILE ".txt" ]
INPUT-FILE: make file! INPUT-FILE
OUTPUT-FILE: make file! OUTPUT-FILE
DIAL-PEERS-NUMBERS: read/lines INPUT-FILE ...
** Access Error: Cannot open /C/Documents and Settings/svondrasek/Desktop/REBOL/owwnumbers.txt.txt
** Near: DIAL-PEERS-NUMBERS: read/lines INPUT-FILE
Steve Vondrasek
--
______________________________________________
Check out the latest SMS services @ http://www.linuxmail.org
This allows you to send and receive SMS through your mailbox.
Powered by Outblaze
[3/10] from: maximo:meteorstudios at: 14-Nov-2003 11:50
this works for me:
INPUT-FILE: ask "enter filename: "
ext: either ((length? INPUT-FILE) > 3) [
copy at INPUT-FILE((length? INPUT-FILE) - 3)
][
""
]
print ext
if ext <> ".txt" [INPUT-FILE: append INPUT-FILE ".txt"]
print INPUT-FILE
;------------------------------------------------
or if using more recent version of rebol core(2.5.2 +)
;------------------------------------------------
input-file: ask "enter filename: "
ext: suffix? input-file
print ext
if ext <> %.txt [input-file: append input-file ".txt"]
print input-file
-MAx
[4/10] from: Steven:White:ci:bloomington:mn:us at: 14-Nov-2003 10:49
Regarding your statements:
INPUT-FILE: make string! INPUT-FILE
etc., are you trying to convert the words INPUT-FILE and OUTPUT-FILE to
strings for examination, and then to file names for reading? If so, I
believe that the "make" function is not the one for the job, and might
be clobbering the values in INPUT-FILE and OUTPUT-FILE. I believe the
proper functions for those operations would be "to-file" and
to-string.
Keeping in mind that I am a beginner...Hope That Helps.
Steven White
City of Bloomington
1800 W Old Shakopee Rd
Bloomington MN 55431-3096
USA
952-563-4882 (voice)
952-563-4672 (fax)
[steven--white--ci--bloomington--mn--us]
>>> [sendit2me--linuxmail--org] 11/14/03 10:06AM >>>
Greetings,
It seems like I always have the hardest time trying to do the
simplest things. For instance in the code snipet below the not equal
comparison EXTENSION-IN <> ".txt" always evaluates to true and always
appends ".txt" to the end of the string even if there's already .txt at
the end of the string. I don't understand. Thanks in advance.
INPUT-FILE: ask "Enter name of file with numbers to be processed: "
comment { user enters owwnumbers.txt }
OUTPUT-FILE: ask "Enter name of output file: "
INPUT-FILE: make string! INPUT-FILE
OUTPUT-FILE: make string! OUTPUT-FILE
comment { Are the last 4 chars of the str equal to ".txt"? If not
append ".txt" to the end of the string. }
EXTENSION-IN: substr INPUT-FILE ( (length? INPUT-FILE ) - 3) 4
print EXTENSION-IN comment { prints out .txt on the screen}
EXTENSION-OUT: substr OUTPUT-FILE ( (length? OUTPUT-FILE ) - 3) 4
if [EXTENSION-IN <> ".txt"] [append INPUT-FILE ".txt" ]
if [EXTENSION-OUT <> ".txt"] [append OUTPUT-FILE ".txt" ]
INPUT-FILE: make file! INPUT-FILE
OUTPUT-FILE: make file! OUTPUT-FILE
DIAL-PEERS-NUMBERS: read/lines INPUT-FILE ...
** Access Error: Cannot open /C/Documents and
Settings/svondrasek/Desktop/REBOL/owwnumbers.txt.txt
** Near: DIAL-PEERS-NUMBERS: read/lines INPUT-FILE
Steve Vondrasek
--
______________________________________________
Check out the latest SMS services @ http://www.linuxmail.org
This allows you to send and receive SMS through your mailbox.
Powered by Outblaze
[5/10] from: tomc:darkwing:uoregon at: 14-Nov-2003 9:08
Howdy,
On Sat, 15 Nov 2003, Steve Vondrasek wrote:
> Greetings,
> It seems like I always have the hardest time trying to do the simplest things.
For instance in the code snipet below the not equal comparison EXTENSION-IN <> ".txt"
always evaluates to true and always appends ".txt" to the end of the string even if there's
already .txt at the end of the string. I don't understand. Thanks in advance.
<<quoted lines omitted: 7>>
> EXTENSION-OUT: substr OUTPUT-FILE ( (length? OUTPUT-FILE ) - 3) 4
> if [EXTENSION-IN <> ".txt"] [append INPUT-FILE ".txt" ]
thing is, the condition in a block is of type block!
and block! is not 'false or 'none so the 'if is 'true.
i.e.
>> type? [false]
== block!
[6/10] 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
[7/10] from: AJMartin:orcon at: 24-Dec-2003 22:47
Steve wrote:
> if [EXTENSION-IN <> ".txt"] [append INPUT-FILE ".txt" ]
One thing that can help in the conversion to Rebol and to aid one's
understanding, is to swap the first square brackets with parenthesis, like:
if (EXTENSION-IN <> ".txt") [append INPUT-FILE ".txt" ]
Andrew J Martin
Speaking in tongues and performing miracles.
ICQ: 26227169
http://www.rebol.it/Valley/
http://valley.orcon.net.nz/
http://Valley.150m.com/
[8/10] from: AJMartin:orcon at: 24-Dec-2003 22:47
> Now, the next step would be to generalize this stuff in case you need
> it again in the future:
<<quoted lines omitted: 6>>
> attempt [if #"." <> first suffix [suffix: join %. suffix]]
> append either s: find string suffix? string [clear s][string]
suffix
> ]
You might find this function helpful:
Extension: function [
"Changes the extension of the path to the specified extension."
Path [file! url!]
Ext [file!] "Like: %.txt"
] [Dot] [
all [
Dot: any [find/last Path #"." tail Path]
not find Dot #"/"
clear Dot
append Path Ext
]
]
Andrew J Martin
Speaking in tongues and performing miracles.
ICQ: 26227169
http://www.rebol.it/Valley/
http://valley.orcon.net.nz/
http://Valley.150m.com/
[9/10] from: antonr::iinet::net::au at: 15-Nov-2003 22:47
I think here is what you want in one line:
if %"" <> find/last/tail INPUT-FILE %.txt [append INPUT-FILE %.txt]
Anton.
[10/10] from: atruter:labyrinth:au at: 15-Nov-2003 23:08
> I think here is what you want in one line:
>
> if %"" <> find/last/tail INPUT-FILE %.txt [append INPUT-FILE %.txt]
Or:
if %.txt <> suffix? file [file: join file %.txt]
Assuming you replace the "ask" with a "to-file ask". ;)
Regards,
Ashley
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted