[Gambas-user] Doesn't IF NOT exist? Bug or feature? Or my bad logic?
Ron_1st
ronstk at ...239...
Thu Sep 17 18:50:07 CEST 2009
On Thursday 17 September 2009, Rolf-Werner Eilert wrote:
> Just stumbled over this when trying to implement a small copying function:
>
> FOR EACH datei IN Dir(opfad, odatei & ".*")
> IF NOT file.Ext(opfad &/ datei) = "lock" THEN
> COPY opfad &/ datei TO ziel &/ datei
> SELECT CASE file.Ext(ziel &/ datei)
> CASE "fehl", "feldbak", "felder", "notizen"
> SHELL "chgrp kartei " & ziel &/ datei WAIT
> SHELL "chmod 660 " & ziel &/ datei WAIT
> CASE "konto", "kontobak", "ktx", "ktxbak"
> SHELL "chgrp konto " & ziel &/ datei WAIT
> SHELL "chmod 660 " & ziel &/ datei WAIT
> END SELECT
> END IF
> NEXT
>
> IF NOT ... = ... THEN
>
> wouldn't copy anything, although only one file has the given extension.
> When I change this line into
>
> IF file.Ext(opfad &/ datei) <> "lock" THEN
>
> everything runs as expected. Doesn't make sense to me as both ways
> should mean the same in my understanding of logical NOT (which doesn't
> mean too much... :-) )
>
> Regards
>
> Rolf
>
Just for information
You have a condition, it can be true or false and this is a boolean value.
NOT inverts the boolean value, here just after it in the line.
a = b
The '=' is a assignment, Set a to the value of b
if a = b then
The '=' is here a relational operator, just as '<', '>' are.
note: in all examples below text between '[' and ']' are optional values/expresions, etc
Syntax diagram for IF...THEN :
IF _relational-expression_ [ _relational-operator_ _relational-expression_ ] THEN
------------------------
_relational-expresion_ :
_expression_ [ _relational-operator_ _expression_ ]
_expression_ :
variable [ arithmetic-operator variable ]
_variable_:
name of the variable
------------------------
About operators:
_relational-operator_ :
any of '<', '=', '>' or combination i.e. '=>', '<=', '=<', '=<'
_logical-operator_ :
a symbol/name for operating on boolean values
& AND
| OR
XOR
NOT
_arithmetic-operator_ :
+ - * / \ % for plus, min, multiply, integerdivide and modulo
-----------------------
Order of operations:
Operations are done in special ordered sequence (presedence)
highest-order
()
as i.e. ( _anything_ [ _any-operator_ _anything_] )
arith-order
^ * / sqr + - (Dutch trick:Meneer Van Dale Wacht Op Antwoord)
relational-order
there is one but I need to look up the correct one.
1 =
2 = < or >
3 = <= or =>
logical-order
OR AND (not sure
Its a long time ago (around 1978) I did write this expresion evaluator
for Basic. Was interesting stuff to do.
For the usage of AND in the IF...THEN you use
variableA AND variableB
variableA OR variableB
variableA XOR variableB
NOT variableA <------------- BE CAREFULL,
only the first and only one (1) following is used for NOT
if you have more following then they must be first evaluated and
need '(' and ')' around it !!
In a asignment as:
bMyVariableC = variableA XOR variableB
or
bMyVariableC = NOT variableA
Look now close to your line.
IF NOT file.Ext(opfad &/ datei) = "lock" THEN
What you say here is,
do a NOT operation on
the boolean value 'file.Ext(opfad &/ datei)'
and compare it to a string "lock"
the parts are
1 NOT file.Ext(opfad &/ datei)
2 =
3 "lock"
You did want however test for the equality of
'file.Ext(opfad &/ datei) = "lock"'
and if this is not equal then use the THEN code.
These are two strings and if they are equal the result is boolean TRUE.
For the inverse you need then use the NOT on the result.
This way you should have used.
IF NOT ( file.Ext(opfad &/ datei) = "lock" ) THEN
I know a very long answer but it just information also
for other how it is more or less is working and why you
got the wrong behaviour in first place.
IF file.Ext(opfad &/ datei) <> "lock" THEN
is the good replacement for the IF NOT ( TRUE ) THEN
Best regards,
Ron_1st
--
More information about the User
mailing list