Jump to content

Seems like a bug..


Recommended Posts

Everything you need to know is in the attached image. I can attach my whole script though if that seems helpful.

I'm reading from excel, the cell reads: "4/16-5/1/19"

I stringsplit the string into array $aDate. $aDate[2] = 16 and $aDate[3] = 5 but when I compare the two, Autoit keeps telling me $aDate[3] is the larger value.

 

I'm sure I must be doing something wrong because every time I think I find a bug, it turns out to be user error. Can anyone help me, please? :)

 

 

I don't understand.PNG

Link to comment
Share on other sites

  • Developers

It is indeed a Bug.... in your script. ;) 
When comparing dates you really need to convert them to a numeric YYYYMMDD format to do a proper numeric comparison....
.. or atleast convert the strings to a number before comparing.

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

10 minutes ago, Jos said:

It is indeed a Bug.... in your script. ;) 
When comparing dates you really need to convert them to a numeric YYYYMMDD format to do a proper numeric comparison.

Jos

This makes sense, however... If my variable/array names weren't "date", you wouldn't even know that I am dealing with dates (you could make an assumption, but the data could be something entirely different). 

 

I don't know how that answers the problem where Autoit is telling me that one array element that equals 5 is larger than another element that equals 16.

 

EDIT: I took your advice (sort of) and it works just fine for me now. 

Instead of: 

$aDate = StringSplit($sDate1, "/-")
  If $aDate[3] > $aDate[2] Then MsgBox("","5 > 16???????",$aDate[3] & " is larger than " & $aDate[2])

I did:

$aDate = StringSplit($sDate1, "/-")
  $2 = $aDate[2] - 1
  $3 = $aDate[3] - 1
  If $3 > $2 Then MsgBox("","5 > 16???????",$aDate[3] & " is larger than " & $aDate[2])

And now it works just fine. I just find the whole thing a bit weird (probably because I don't understand things).

Edited by Flaccid_Jack
To add information
Link to comment
Share on other sites

  • Developers
9 minutes ago, Flaccid_Jack said:

I don't know how that answers the problem where Autoit is telling me that one array element that equals 5 is larger than another element that equals 16.

13 minutes ago, Jos said:

. or atleast convert the strings to a number before comparing.

Post a simple script that shows your issue and we'll explain why it is happening and how to solve it. :) 

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Just now, Jos said:

Post a simple script that shows your issue and we'll explain why it is happening and how to solve it. :) 

Jos

#include <Array.au3>
#include <Excel.au3>
#include <GUIConstantsEx.au3>
#include <Constants.au3>
#include <MsgBoxConstants.au3>
#include <Date.au3>
#include <Math.au3>
#include <WindowsConstants.au3>
#include <WinAPIError.au3>
#include <StringConstants.au3>
#include <WinAPIFiles.au3>
MsgBox("","Attention","Please prepare Coaches Payroll.xlsx")
Local $oExcel = _Excel_Open()
Local $sWorkbook = @ScriptDir & "\Coaches Payroll.xlsx"
Local $oWorkbook = _Excel_BookOpen($oExcel, $sWorkbook ,0,Default,"VBCoach2761",Default)
If @error Then MsgBox("","Attention!","We could not find a file with the title 'Coaches Payroll.xlsx' on your desktop!")
$oWorkbook.Sheets(2).Activate
Local $aWorkbook = _Excel_RangeRead($oWorkbook, Default, $oWorkbook.ActiveSheet.Usedrange.Columns("A:AZ"))
For $i = 0 To UBound($aWorkbook) - 1 ;;;;;;;;;;;; Get Coaches for Coaches payroll
   If $aWorkBook[$i][4] <> "" Then
      If $aWorkBook[$i][4] < 1000000000 Then
         $sDate1 = $aWorkBook[$i][4]
         $aDate = StringSplit($sDate1, "/-")
         If $aDate[3] > $aDate[2] Then MsgBox("","5 > 16???????",$aDate[3] & " is larger than " & $aDate[2])
      EndIf
   EndIf
Next

I gutted the script to simplify it and gutted the excel file for confidential reasons but it should still show the problem. 

Also, I don't know if you saw my edit, Jos, on my second post, but I do have my script working, I'm just curious at this point. Thanks!

Coaches Payroll.xlsx

Link to comment
Share on other sites

  • Developers

I was more thinking about something simple as this to demonstrate my point:

Global $Array[] = ["5","16"]
If $Array[0] > $Array[1] Then
    ConsoleWrite( $Array[0] & " is bigger than " & $Array[1] & @crlf)
Else
    ConsoleWrite( $Array[0] & " is not bigger than " & $Array[1] & @crlf)
EndIf

If Number($Array[0]) > Number($Array[1]) Then
    ConsoleWrite( $Array[0] & " is bigger than " & $Array[1] & @crlf)
Else
    ConsoleWrite( $Array[0] & " is not bigger than " & $Array[1] & @crlf)
EndIf

Have a look and see if that makes sense. First If is an Alphanumeric compare (2 strings) and second a numeric compare.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

8 minutes ago, Jos said:

I was more thinking about something simple as this to demonstrate my point:

Global $Array[] = ["5","16"]
If $Array[0] > $Array[1] Then
    ConsoleWrite( $Array[0] & " is bigger than " & $Array[1] & @crlf)
Else
    ConsoleWrite( $Array[0] & " is not bigger than " & $Array[1] & @crlf)
EndIf

If Number($Array[0]) > Number($Array[1]) Then
    ConsoleWrite( $Array[0] & " is bigger than " & $Array[1] & @crlf)
Else
    ConsoleWrite( $Array[0] & " is not bigger than " & $Array[1] & @crlf)
EndIf

Have a look and see if that makes sense. First If is an Alphanumeric compare (2 strings) and second a numeric compare.

Jos

I don't understand why you're posting this. Autoit logic is SUPPOSED to work like this. My question is with my specific code and why it doesn't seem to be working. I don't mean to be rude but I feel like you don't understand why my script is working the way it is. Or maybe you didn't actually run it. 

 

Either way, you've been helpful and my script works, so I thank you for that! 

Link to comment
Share on other sites

  • Developers
1 minute ago, Flaccid_Jack said:

Or maybe you didn't actually run it. 

Correct, as I didn't need to to demonstrate the issue with typecasting and comparing Strings. 
I am lost as far as your first sentence about Autoit3 and then you state it does solve your issue?

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

9 minutes ago, Flaccid_Jack said:

My question is with my specific code and why it doesn't seem to be working.

Because in your code you're comparing 2 strings, just because those strings contain characters that are also considered to be numbers, doesn't mean they're treated like numbers, they're treated like strings. Look up lexicographical comparison for an explanation as to why.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...