Evaluating multiple OR conditions - julia

Due to short circuit rules I can't write something like the this:
message = "nada"
if message != "success" || message != "login successful"
error("get_response() failed with message returned of: $message")
end
Instead I've had to write the something like this to get it working:
message = "nada"
if message != "success" ? message != "login successful" : false
error("get_response() failed with message returned of: $message")
end
The second way seems ... "clunky", so I feel like I've missed something when going through the julia manual. Is there a standard way to write multiple OR conditions? (or a better way to write multiple OR conditions than what I have).
Assume the message could be any string, and I only want to do something if it isn't "success" or "login successful".

This simply seems to be a mistake in the use of or/and vs negations. I guess that what you wanted was
message = "nada"
if !(message == "success" || message == "login successful")
error("get_response() failed with message returned of: $message")
end
or equivalently
message = "nada"
if message != "success" && message != "login successful"
error("get_response() failed with message returned of: $message")
end
which both work for me. The original condition
message != "success" || message != "login successful"
is always true since any message has to be unequal to at least one of the strings "success" and "login successful".

Related

Inputbox and cases, cancel or closing the window

I'm new to autoit, and I'm wondering how to deal with some things. The code is simply this line :
$input = InputBox("game : +/-", "Write a number:")
If I write a number in the section, the program goes normaly.
If I click the cancel button, an error is thrown and so I dealt with it with :
If (#error = 1) Then
$End = True
Is what I ve done okay?
And :
Could you please explain what is going on here and what exactly is happening if I enter no value or if I press cancel?
If I close the windows, what happens ? I'd like the program to end.
Thank you very much ! Sorry if my question is easy or useless, I'll help me a lot
with a couple of ternary ops you can see that the cancel button sets the error flag and it does =1 or ==1 or =True (because True evaluates to 1)
$input = InputBox("game : +/-", "Write a number:")
$result = (#error == 1) ? 'cancel was pressed' : $input
msgbox(0, '' , $result = '' ? 'empty string' : $input)
When you call the InputBox function values are returned to indicate the result of the process these are:
Success: the string that was entered.
Failure: "" (empty string) and sets the #error flag to non-zero.
#error: 1 = The Cancel button was pushed. 2 = The Timeout time was
reached. 3 = The InputBox failed to open. This is usually caused by
bad arguments. 4 = The InputBox cannot be displayed on any monitor. 5
= Invalid parameters width without height or left without top.
So essentially that means that if it returns a non-empty string, the "success" case, you don't have to worry about #error. If any non-zero value is returned, the value of #error will indicate what has happened. So if in the case of an error you just want to return, you should use this if statement:
If (#error <> 0) Then
$End = True
This works because we know if #error == 0 then the input box has been successful and a value has been returned, otherwise we know it's thrown one of the errors listed above. I would anticipate that closing the window has the same effect as pressing cancel, i.e. #error == 1 but I haven't checked.
Further to this, if you wanted to you could switch on the value of #error and use that to give the user an error message along the lines of "please enter a value" or "command timed out" but that seems more than is required in this instance.
Here's the relevant documentation: https://www.autoitscript.com/autoit3/docs/functions/InputBox.htm
Good luck!

Is possible to add a String.contains more than one value?

I want to make a control, when I create a companyId, to not permit to create id with special characters like, (&), (/), (), (ñ), ('):
If txtIdCompany.Text.Contains("&") Then
// alert error message
End If
But I can't do this:
If txtIdCompany.Text.Contains("&", "/", "\") Then
// alert error message
End If
How can I check more than one string in the same line?
You can use collections like a Char() and Enumerable.Contains. Since String implements IEnumerable(Of Char) even this concise and efficient LINQ query works:
Dim disallowed = "&/\"
If disallowed.Intersect(txtIdCompany.Text).Any() Then
' alert error message
End If
here's a similar approach using Enumerable.Contains:
If txtIdCompany.Text.Any(AddressOf disallowed.Contains) Then
' alert error message
End If
a third option using String.IndexOfAny:
If txtIdCompany.Text.IndexOfAny(disallowed.ToCharArray()) >= 0 Then
' alert error message
End If
If txtIdCompany.Text.Contains("&") Or txtIdCompany.Text.Contains("\") Or txtIdCompany.Text.Contains("/") Then
// alert error message
End If

How to handle failed XPATH lookup in MSXML from AutoIT?

I am parsing a piece of XML returned from a Web API. I am looking for a particular node. If that node does not exist, according to the MSXML documentation, it returns null.
The problem is, I don't know how to check for null in AutoIT. I have read the online API doc for Null, but when I run the script using AutoIt3Wrapper v.2.1.2.9, it does not recognize null.
Here is a sample script to show what I mean:
$oXMLDOM = ObjCreate("Msxml2.DOMDocument.3.0")
$xml = '<response><error code="1"><![CDATA[ Incorrect password or username ]]></error></response>'
$oXMLDOM.loadXML($xml)
$node = $oXMLDOM.selectSingleNode("/response/error")
MsgBox(0, "", $node.text) ;; No problems
$node = $oXMLDOM.selectSingleNode("/response/token")
;; $node should be 'null' now; how do I check that in AutoIT?
MsgBox(0, "", $node.text) ;; Fails horribly
You could use IsObj() to test if a valid object was returned:
If Not IsObj($oNode) Then
MsgBox(0, 'ERROR', 'Node is invalid!')
EndIf
I have kind of found a quick workaround for my problem.
By using ObjName(), I can check the name of the COM object returned, which is IXMLDOMElement if it is successful:
If ObjName($node) = "IXMLDOMElement" Then
MsgBox(0, "", "Success")
Else
MsgBox(0, "", "Failure")
EndIf

What if TinyURL API doesn't work..?

I have a vbscript function to create a tinyurl from a regular url.
FUNCTION GetShortURL(strUrl)
Dim oXml,strTinyUrl,strReturnVal
strTinyUrl = "http://tinyurl.com/api-create.php?url=" & strUrl
set oXml = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0")
oXml.Open "GET", strTinyUrl, false
oXml.Send
strReturnVal = oXml.responseText
Set oXml = nothing
GetShortURL = strReturnVal
END FUNCTION
I have come across the problem when the tinyurl api is down or inaccessible, making my script fail:
msxml3.dll
error '80072efe'
The connection with the server was terminated abnormally
Is there a safeguard I can add to this function to prevent the error and use the long url it has..?
Many thanks in advance,
neojakey
If you want to just return strUrl if the call fails, you can use On Error Resume Next
FUNCTION GetShortURL(strUrl)
on error resume next
Dim oXml,strTinyUrl,strReturnVal
strTinyUrl = "http://tinyurl.com/api-create.php?url=" & strUrl
set oXml = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0")
oXml.Open "GET", strTinyUrl, false
oXml.Send
strReturnVal = oXml.responseText
Set oXml = nothing
'Check if an error occurred.
if err.number = 0 then
GetShortURL = strReturnVal
else
GetShortURL = strUrl
end if
END FUNCTION
Obviously, you need some kind of error handling. In general, VBScript's error handling is no fun, but for your specs - get the right thing or the default in case of any/don't care what problem - you can use a nice and simple approach:
Reduce your original function to
assignment of default value to function name (to prepare for the failures)
On Error Resume Next (to disable VBScript's default error handling, i.e. crash)
assignment of the return value of a helper function to function name (to prepare for success)
In code:
Function GetShortURL2(strUrl)
GetShortURL2 = strUrl
On Error Resume Next
GetShortURL2 = [_getshorturl](GetShortURL2)
End Function
I use [] to be able to use an 'illegal' function name, because I want to emphasize that _getshorturl() should not be called directly. Any error in _getshorturl() will cause a skip of the assignment and a resume on/at the next line (leaving the function, returning the default, reset to default error handling).
The helper function contains the 'critical parts' of your original function:
Function [_getshorturl](strUrl)
Dim oXml : Set oXml = CreateObject("Msxml2.ServerXMLHTTP.3.0")
oXml.Open "GET", "http://tinyurl.com/api-create.php?url=" & strUrl, False
oXml.Send
[_getshorturl] = oXml.responseText
End Function
No matter which operation fails, the program flow will resume on/at the "End Function" line of GetShortURL2().
Added: Comments on the usual way to do it (cf. Daniel Cook's proposal)
If you switch off error handling (and "On Error Resume Next" is just that) you are on thin ice: all errors are hidden, not just the ones you recon with; your script will do actions in all the Next lines whether necessary preconditions are given or preparations done.
So you should either restrict the scope to one risky line:
Dim aErr
...
On Error Resume Next
SomeRiskyAction
aErr = Array(Err.Number, Err.Description, Err.Source)
On Error Goto 0
If aErr(0) Then
deal with error
Else
normal flow
End If
or check after each line in the OERN scope
On Error Resume Next
SomeRiskyAction1
If Err.Number Then
deal with error
Else
SomeRiskyAction2
If Err.Number Then
deal with error
Else
...
End If
End If
That's what I meant, when I said "no fun". Putting the risky code in a function will avoid this hassle.

Problem with email validation: Invalid procedure call or argument: 'Mid'

I tried to control email address and reviewer's name with the following code but I received this error.
Microsoft VBScript runtime error '800a0005'
Invalid procedure call or argument: 'Mid'
Cant I compare Mid(REVIEWEREMAIL, InStr(1, REVIEWEREMAIL, "#", 1), 1) to "#"?
If Len(REVIEWERNAME) < 2 Then
with response
.write "Error! Please fill in valid name. <br />"
end with
ElseIf Len(REVIEWEREMAIL) < 3 Then
with response
.write "Error! Please fill in valid email address. <br />"
end with
ElseIf Mid(REVIEWEREMAIL, InStr(1, REVIEWEREMAIL, "#", 1), 1) <> "#" Then
with response
.write "Error! Please fill in valid email address. <br />"
end with
Else
insert...
End If
You can simplify that last ElseIf like this:
ElseIf InStr(REVIEWEREMAIL, "#", vbTextCompare) = 0 Then
Because as written, you're solely checking if there is a # in the email address provided.
If you're concerned about further validating the email addresses you get, there is a great deal written about using regular expressions to validate email addresses. One example: http://www.codetoad.com/asp_email_reg_exp.asp
The likely reason is that REVIEWEREMAIL is null -- in which case your first IF condition isn't going to catch it as intended. Len() of a null doesn't return an integer 0.
The "classic" ASP cheat for that is to change your null variable to an empty string with something like
REVIEWEREMAIL = REVIEWEREMAIL & ""
which will give back LEN = 0
Beyond that, you may want to look for a regular expression online that will check for valid email values.

Resources