• Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r4wp

[Rebol School] REBOL School

PatrickP61
2-May-2013
[1875]
Quick Question: I'm having some trouble understanding ANY.  I thought 
it meant that you could state a condition such as IF var = ANY ["val1" 
"val2"] [do something]

But that only becomes true on the first condition, not the second. 
 Example code:


>> forever [do cmd: ask "? " if cmd = any ["quit" "q"] [print "done" 
break]]
? q
? quit
done
>>
what am I missing?
MikeL
2-May-2013
[1876x3]
The first value in your ANY block is "Quit" which is not a TRUE.
The second is "Q" which is also not a TRUE.
HELP ANY gives Evaluates and returns the first value that is not 
FALSE or NONE.
Gregg
2-May-2013
[1879]
Patrick, just do this in the console and see what you get:

any ["quit" "q"]
PatrickP61
2-May-2013
[1880]
Ok, so ANY is simply a way to pass back a value that is not false?
Gregg
2-May-2013
[1881x2]
The key part in the doc string: "...returns the first value that 
is not FALSE or NONE"
So it evaluates each value in the block you give it, and as soon 
as it hits a value that is not false or none, it returns that value. 
So, in your case, it will always return "quit".
PatrickP61
2-May-2013
[1883]
so in my case ANY ["quit" "q"] will ALWAYS return "quit" since it 
is a valid expression
Gregg
2-May-2013
[1884x2]
Correct.
So think of how to solve it other ways. ANY evaluates, so you could 
put your comparison expressions inside the ANY block. That will get 
unwieldy very fast though. How else might you do it? What is it you 
want to do (in a general sense)?
PatrickP61
2-May-2013
[1886x2]
I suppose the CASE command would be better
My goal is to simply stay in a loop, accepting all valid rebol commands 
until I quit the loop

For example, I've defined some variables to script names, and so 
when I enter the variable, it should DO those names, until quit
Gregg
2-May-2013
[1888x2]
And for the case of q and quit, and maybe others, you want to see 
if they command is in a set of commands you want to process in a 
special way, correct?
That is, you want to see if you FIND the command in a set of known 
commands and act accordingly.
PatrickP61
2-May-2013
[1890x2]
not really, it's just a quick shortcut to allow any REBOL command 
to be performed while it is in a script that is already executing.
...doing stuff...

stopping here to accept various rebol commands and looping until 
finished
...doing the rest of the script
Its a way to "pause" a running script to enter rebol commands and 
then when I'm done, get out of the loop and continue with the rest 
of the script
Gregg
2-May-2013
[1892]
Right, so you ask for the command, see if it's a command to exit 
your loop (q or quit) and DO it if not, correct?
PatrickP61
2-May-2013
[1893x2]
yep
Although, the way I have it, it should do the command no matter what 
is entered
Gregg
2-May-2013
[1895x2]
And it will, which is not what you want.
So start with that, and then add the handling for your special commands.
PatrickP61
2-May-2013
[1897]
Actually, it is.  I want it to do ANY rebol command.
Gregg
2-May-2013
[1898x3]
forever [cmd: ask "? "  do cmd]
Any command except q or quit, which should break your loop, correct?
And what about an empty command?
PatrickP61
2-May-2013
[1901x3]
Doesn't work
It just repeats what i've entered
forever [do ask "? "]  will work on some rebol commands like HELP 
or PRINT "HI" but not "Q" or "QUIT"

which is why I tried to state the condition to break out of the loop
Gregg
2-May-2013
[1904]
So if you type "quit" (no quotes) after running that, it doesn't 
quit REBOL?
PatrickP61
2-May-2013
[1905x2]
right
it is like the word is not being evaluated
Gregg
2-May-2013
[1907]
What version of REBOL, and what OS? It works as expected here on 
Win7.
PatrickP61
2-May-2013
[1908]
r3 111 vista
Andreas
2-May-2013
[1909]
Was just askin about R3 vs R2 :/ I fear you hit a known bug in R3.
Gregg
2-May-2013
[1910x2]
Ah, R3. Let me try.
Indeed. I now see your behavior.
PatrickP61
2-May-2013
[1912]
thanks Andreas,  that explains it
Andreas
2-May-2013
[1913]
(http://issue.cc/r3/851-- that's the bug, just in case.)
PatrickP61
2-May-2013
[1914]
Just FYI: workaround the bug:

forever	[
	do script: ask	[~ " REBOL COMMAND? "]	; do the entered command
	if script = "q"		[break]
	if script = "quit"	[break]	
	]
Gregg
2-May-2013
[1915x3]
So, coming back to the original question... :-) Rather than doing 
"script =" for every command, you could say "if find ["quit" "q"] 
cmd [break]", so it's easier to check multiple commands, aliases, 
etc. e.g., what about HALT?
So I would still hold the result of ASK in a temp var, rather than 
DOing it directly and relying on the bug's behavior.
i.e., get the command, check if it's special and, if not, DO it.
PatrickP61
2-May-2013
[1918]
Yes, that would work better
Endo
4-May-2013
[1919]
May be we should create a "R3 School" and rename this group to "R2 
School"?

So we do not have to ask for version for every question asked in 
this group. What do you think?
Gregg
4-May-2013
[1920]
I think this group is fine. Otherwise we may have fragmentation about 
general questions.
PatrickP61
7-May-2013
[1921]
In R3, I want to be able to request a specific directory quickly 
and easily.  The easiest way I've found is to use REQUEST-FILE since 
it allows the user to quickly "navigate to" the desired directory. 
 Thing is, it requires the user to pick an existing file , even though 
I don't care about the file itself.  In most cases, this is fine 
since the user can pick any one of the files, but in cases where 
a directory is empty of a file, I have a problem.  

example code:

request-file/file to-file join "/" second parse what-dir "/"   <-- 
I use this to default the directory at the highest level is   ie 
 %/c  

Is there a better way to do this in R3?
Endo
7-May-2013
[1922]
There is also request-dir I think.
PatrickP61
7-May-2013
[1923]
I'm using 2.100.111 and no REQUEST-DIR, unless it was added in a 
later version
Cyphre
7-May-2013
[1924]
In Saphirion's build we have REQUEST-DIR already added.