SadBunny

truth value of a variable containing an array is True, False, both, neither?

11 posts in this topic

#1 ·  Posted (edited)

Hi there,

In another thread Nitekram had a function returning either a populated array or False. I was about to suggest you could still use that as a boolean, but I'm happy I tested that assumption just to be sure.

dim $a[1];
$a[0]="hello world";

if $a <> False then
    ConsoleWrite("it is something other than False" & @crlf)
EndIf

if $a then
    ConsoleWrite("it is true" & @crlf)
Else
    ConsoleWrite("it is not true" & @crlf)
EndIf

if not $a then
    ConsoleWrite("it is false" & @crlf)
Else
    ConsoleWrite("it is not false" & @crlf)
EndIf
Result:
it is something other than False
it is not true
it is false

Argh! What is the boolean truth value of a variable containing a perfectly fine array? Seemingly it's a meaningless question in AutoIt... Really??

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

Argh! What is the boolean truth value of a variable containing a perfectly fine array?

i think your missing the point.

a variable doesnt containing a perfectly fine array

a array containing a perfectly fine array elements

so i googled this for you that i think it'll be satisfying for you

http://www.answers.com/Q/What_is_the_difference_between_an_array_and_variable

especialy

"it is true when the elements of the array are treated as individual entities"

and

"It is not generally right to distinguish between a variable and an array. You'd be comparing apples with pears."

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

 

I understand that I can check whether a variable points to an array with IsArray(), but that's not the point. I myself would consider a situation where you use a variable both as a boolean and a pointer to an array bad code. I would not run into this problem in my work :) I just think it's weird that AutoIt works this way.

 

i think your missing the point.

a variable doesnt containing a perfectly fine array

a array containing a perfectly fine array elements

 

Thanks for the link, but I think you're missing the point :) I did and would not confuse a variable with an array. For the purposes of this question, a variable is a handle to a certain data structure. Whether that be a boolean or an integer or an array (itself a collection of handles to yet other data structures) or an Audi sports car is not important. If I put it behind an if, I expect the engine to either draw an understandable boolean from it (meaning based on an understandable algorithm), or error out with "invalid truth expression" or whatever. Take awk, mine gives a clear error if I try to use a variable "holding" an array as the truth expression:

$ awk 'BEGIN {arr[1]=1; if (arr){print "yes";}}'
awk: cmd. line:1: fatal: attempt to use array `arr' in a scalar context

Or perl, which considers an empty array false and a non-empty array true, which also makes a lot of sence IMO:

$ perl -e "@arr = (); if (@arr) {print 'yes'}"

$ perl -e "@arr = (0,1,2); if (@arr) {print 'yes'}"
yes

AutoIt seems weird in this regard.

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

It's a bit confusing. If I create an array with one False value, it still exists. I would return True (not False because the array contains information). What if an array has zero elements or a mixture of True and False values?

Edited by czardas

Share this post


Link to post
Share on other sites

Hi,

It is well known that because of its lack of typing AutoIt can produce some apparently strange results when comparing variables - think of the number of times we have had to explain about the problems of comparing stings and numeric values. It seems to me that we have another case in point here - trying to compare arrays with Booleans is just not a sensible thing to do. The Help file does say for True/False:

 

These keywords should not be used in expressions as AutoIt will not detect this 'misuse' and the results will be unpredictable

which seems to sum up the results obtained here quite nicely. :)

It seems to me that the entire matter could have been avoided had the function to which the OP referred used a more sensible error return value than False allowing for a simple and logical comparison to detect the failure case - for instance returning an empty string, or indeed setting the @error macro which is designed for this very case. ;)

M23

1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

It's a bit confusing. If I create an array with one False value, it still exists. I would return True (not False because the array contains information). What if an array has zero elements or a mixture of True and False values?

 

If I had to choose, I'd agree with the Awk approach, and would prefer a language to throw some sort of type safety error, even a language without much of a type safety idea (which Awk also doesn't have). Perl is stupid anyway (apart from its stellar Regex support), but at least it's consistently stupid :) Lacking a null or a strict boolean type in the language, I could also live with considering any variable pointing to an empty string ("") or integer 0 a False, and anything else (including an array, even an empty one) as True. But defending that still leads to arguments that feel like deductive contradictions, so it would still itch :)

 

Hi,

It is well known that because of its lack of typing AutoIt can produce some apparently strange results when comparing variables - think of the number of times we have had to explain about the problems of comparing stings and numeric values. It seems to me that we have another case in point here - trying to compare arrays with Booleans is just not a sensible thing to do. The Help file does say for True/False:

 

These keywords should not be used in expressions as AutoIt will not detect this 'misuse' and the results will be unpredictable

which seems to sum up the results obtained here quite nicely. :)

It seems to me that the entire matter could have been avoided had the function to which the OP referred used a more sensible error return value than False allowing for a simple and logical comparison to detect the failure case - for instance returning an empty string, or indeed setting the @error macro which is designed for this very case. ;)

M23

 

 

All very True of course :) (/edit: then again... what does True even mean given the subject matter of this thread? :sorcerer:  ) And I understood that, and there are multiple ways to improve that function I referred to. Yours is fancy, my own quite oneliner-y suggestion in that thread probably came from 10 hours of awk'ing radiation at the black hole that was my work today. Still I think it's kind of strange that you can have something of which you are allowed to ask the truth value, but still get a boolean paradox. Any choice for a default algorithm in this case is better than a paradox, I would say?

Not that I am complaining here, mind you... I never ran into this situation, and I never would have if I wasn't inspired by someone else's code to deliberately test it :) I'm just somewhat surprised it's a feature instead of a bug.

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

I don't understand this statement:

These keywords should not be used in expressions as AutoIt will not detect this 'misuse' and the results will be unpredictable.

 

Is the following not an expression?

Not ($bBoolean = True)
 

Edited by czardas

Share this post


Link to post
Share on other sites

czardas,

I read the statement to mean that you should not mix booleans with other datatypes in the same expression - so what you have there is perfectly valid as it is all boolean. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Melba23: What you said there is more comprehensive than the help file. Perhaps stating that as a caution would be okay, since a certain amount of internal casting does take place when performing a comparison using the equals operator.

Share this post


Link to post
Share on other sites

czardas,

 

What you said there is more comprehensive than the help file

I should have made clear that the comment is a purely personal opinion. As I have no access to the internal AutoIt code, I can only base my comments on my own experiences of coding in AutoIt. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now