Modify

Opened 14 years ago

Closed 14 years ago

#1733 closed Feature Request (Rejected)

IsEmpty function

Reported by: josh_petitt@… Owned by: Jon
Milestone: Component: AutoIt
Version: Severity: None
Keywords: Cc:

Description

Hello,

Here is a function I find helpful:





; Returns True if value is "empty" (i.e. exists, but no data).
Func IsEmpty($value = '')
	Select
		Case IsString($value)
			Return $value == ''

		Case IsArray($value)
			Return UBound($value) = 0

		Case IsNumber($value) Or IsBinary($value) Or IsPtr($value)
			Return $value = 0

		Case IsBool($value)
			Return $value = False

		Case True
			Return $value ; if don't know the object type, just pass it on
	EndSelect
EndFunc   ;==>IsEmpty




It is just a suggestion, thanks!

Attachments (0)

Change History (14)

comment:1 Changed 14 years ago by anonymous

The last test could also be:

Case True
Return Not $value

I will leave it up to the experts as to which is correct :-)

comment:2 Changed 14 years ago by TicketCleanup

  • Version 3.3.6.0 deleted

Automatic ticket cleanup.

comment:3 Changed 14 years ago by Jpm

the coding seems a little complicated as the following do the same

Local $a[1]
Local $array = IsEmpty( $a )
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $array = ' & $array & @crlf) ;### Debug Console
Local $str = IsEmpty( 'a' )
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $str = ' & $str & @crlf) ;### Debug Console

Local $number = IsEmpty(1)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $number = ' & $number & @crlf) ;### Debug Console
Local $bin = IsEmpty( Binary(1) )
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $bin = ' & $bin & @crlf) ;### Debug Console
Local $ptr = IsEmpty( Ptr(1) )
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $ptr = ' & $ptr & @crlf) ;### Debug Console
Local $bool = IsEmpty( True )
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $bool = ' & $bool & @crlf) ;### Debug Console
Local $keyword = IsEmpty( Default )
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $keyword = ' & $keyword & @crlf) ;### Debug Console

Local $emptystr = IsEmpty(  '' )
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $emptystr = ' & $emptystr & @crlf) ;### Debug Console
Local $emptynumber = IsEmpty(0)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $emptynumber = ' & $emptynumber & @crlf) ;### Debug Console
Local $emptybin = IsEmpty( Binary(0) )
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $emptybin = ' & $emptybin & @crlf) ;### Debug Console
Local $emptyptr = IsEmpty( Ptr(0) )
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $emptyptr = ' & $emptyptr & @crlf) ;### Debug Console
Local $emptybool = IsEmpty( False )
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $emptybool = ' & $emptybool & @crlf) ;### Debug Console

Func IsEmpty($value = '')
	If IsString($value) Then
		Return $value == ''
	Else
		Return $value = 0
	EndIf
EndFunc

comment:4 Changed 14 years ago by mvg

Don't know what the general context for the IsEmpty() is. But I think its a ambiguous problem at best. Best solved in relation to the variable/code/target in play at the moment(code location/context).

  • There are no empty array's in autoit. A array always has some size.
  • Zero as number can be valid data, as such its not empty.

comment:5 Changed 14 years ago by Jpm

  • Owner set to Jon
  • Status changed from new to assigned

I share mvp remarks, but I leave to other Dev's the final decision oon this request.

comment:6 Changed 14 years ago by Spiff59

I'd vote for documenting in the "Operators" section of the help file the difference between "=" and "==". That, other than case-sensitivity, "=" treats a null (empty) string as equal to 0, and "==" does not.

I don't see reason for a Function that replaces 1 or 2 lines of code.
Where's Valik when ya need him? Gong!

comment:7 Changed 14 years ago by Jpm

@Spiff59
I agree "Operators" section can be improved.
Can you suggest the exact change you want to do?
Thanks

comment:8 Changed 14 years ago by Spiff59

Well, I don't want to create terminology problems, as I've had two persons argue that, in their opinion, an empty string ("") cannot be called a "null string". They attended to a differnt school than I did. Anyway, I'll aviod using the word null regarding a string, or even referencing Chr(0) as the NUL character. So, Maybe...

The current documentation is:
{{{Language Reference - Operators

Tests if two values are equal (case insensitive if used with strings)

Tests if two values are equal (case sensitive if used with strings)}}}

Would be more informative as:

=  Tests if two values are equal (Case-insensitive. Values of 0, "" and Chr(0) are equal)
== Tests if two values are equal (Case-sensitive. Values of 0, "" and Chr(0) are not equal)}}}

???

comment:9 Changed 14 years ago by Jpm

Thanks,
I think doc need to be a little more precise as 'a' = 0 is true too

comment:10 Changed 14 years ago by Spiff59

Wow! I was completely unaware of that one. And I've of NEVER guessed it would be true unless I'd actually encountered it. I have no idea why that comparison is allowed to test as true. Both ASCII Chr(65) and Chr(97) equal 0? I've never encountered that in any language. It must be that there is some reason for this behavior?

comment:11 Changed 14 years ago by anonymous

From doc:

Language Reference - Datatypes

In AutoIt there is only one datatype called a Variant. A variant can contain numeric or string data and decides how to use the data depending on the situation it is being used in. For example, if you try and multiply two variants they will be treated as numbers, if you try and concatenate (join) two variants they will be treated as strings.

But there is no info on ambiguous cases. Like when comparing strings against numbers.
Like with ('a' = 0) -> (number('a') = 0), ('a' = 0) == (0 = 'a').
Seems like "string to number" takes precedence over "number to string" in those cases. (not futher tested)

Something to the effect of:
"In ambiguous (compare?) cases "string to number" is given priority over "number to string" (convertion?)." perhaps.

comment:12 Changed 14 years ago by Spiff59

Maybe I was making it too complicated... After all, "" is a string (an empty one), Chr(0) is a string...

Maybe just add to the documentation that:

"Strings equate to 0." for the = operator

And:

"Strings do not equate to 0." for the == operator

?

The documentation currently only mentioning case-sensitivity is incomplete.

comment:13 Changed 14 years ago by Jpm

Doc is OK for string to string comparison.
I think that needs to be improved is the fact that if it is not a string to string then a conversion is done as Number() do.

comment:14 Changed 14 years ago by Jon

  • Resolution set to Rejected
  • Status changed from assigned to closed

Guidelines for posting comments:

  • You cannot re-open a ticket but you may still leave a comment if you have additional information to add.
  • In-depth discussions should take place on the forum.

For more information see the full version of the ticket guidelines here.

Add Comment

Modify Ticket

Action
as closed The owner will remain Jon.
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.