Jump to content

_ArrayInsert and indexes to insert items


Recommended Posts

Just curious to see if any one else has run into this issue.  When creating list of index, to insert items at in _ArrayInsert, all the indexes have to be the same number of digits or i.t fails with @error = 3

If you set the range to 3;5;7 it works, if its set to 10,15,17 it works, if you set it to 3;15;17 fails if you set it to 03,15,17 it works.  

Here is an example that shows this.

; have the same issues when running on 
; AutoIT Version 3.3.14.5
; AutoIT Beta Version 3.3.15.0

#include <Array.au3>

Local $aTest[151]=[]

For $i = 0 To 150
    $aTest[$i] = 'Line_' & $i
Next

; this works - @error = 0
Local $sIdx = '3;5;7;9'
_ArrayInsert($aTest, $sIdx, 'Added Item 1|Added Item 2|Added Item 3|Added Item 4')
MsgBox($MB_OK, 'Example 1', '_ArrayInsert returned error : ' & @error & @CRLF & 'with $sIdx = ' & $sIdx)

; this works - @error = 0
$sIdx = '13;15;17;19'
_ArrayInsert($aTest, $sIdx, 'Added Item 1|Added Item 2|Added Item 3|Added Item 4')
MsgBox($MB_OK, 'Example 2', '_ArrayInsert returned error : ' & @error & @CRLF & 'with $sIdx = ' & $sIdx)

; this does not work  - @error = 3
$sIdx = '3;5;17;19'
_ArrayInsert($aTest, $sIdx, 'Added Item 1|Added Item 2|Added Item 3|Added Item 4')
MsgBox($MB_OK, 'Example 3', '_ArrayInsert returned error : ' & @error & @CRLF & 'with $sIdx = ' & $sIdx)

; this works - @error = 0
$sIdx = '03;05;17;19'
_ArrayInsert($aTest, $sIdx, 'Added Item 1|Added Item 2|Added Item 3|Added Item 4')
MsgBox($MB_OK, 'Example 4', '_ArrayInsert returned error : ' & @error & @CRLF & 'with $sIdx = ' & $sIdx)

; this does not work - @error = 3
$sIdx = '13;15;107;109'
_ArrayInsert($aTest, $sIdx, 'Added Item 1|Added Item 2|Added Item 3|Added Item 4')
MsgBox($MB_OK, 'Example 5', '_ArrayInsert returned error : ' & @error & @CRLF & 'with $sIdx = ' & $sIdx)

; this works - @error = 0
$sIdx = '013;015;107;109'
_ArrayInsert($aTest, $sIdx, 'Added Item 1|Added Item 2|Added Item 3|Added Item 4')
MsgBox($MB_OK, 'Example 6', '_ArrayInsert returned error : ' & @error & @CRLF & 'with $sIdx = ' & $sIdx)

; this does not work - @error = 3
$sIdx = '3;15;107;109'
_ArrayInsert($aTest, $sIdx, 'Added Item 1|Added Item 2|Added Item 3|Added Item 4')
MsgBox($MB_OK, 'Example 7', '_ArrayInsert returned error : ' & @error & @CRLF & 'with $sIdx = ' & $sIdx)

; this works - @error = 0
$sIdx = '003;015;107;109'
_ArrayInsert($aTest, $sIdx, 'Added Item 1|Added Item 2|Added Item 3|Added Item 4')
MsgBox($MB_OK, 'Example 8', '_ArrayInsert returned error : ' & @error & @CRLF & 'with $sIdx = ' & $sIdx)

 

Link to comment
Share on other sites

I think you may have found a bug.  The @error = 3, in this case, is because it thinks that the indexes are out of order.  It thinks that because it is doing string compares instead of numeric compares.

 

If line #623, in the array.au3, is changed to look like the line below, it works fine.

If Number($vRange[$i]) < Number($vRange[$i - 1]) Then Return SetError(3, 0, -1)

 

Edited by TheXman
Link to comment
Share on other sites

  • Moderators

dave12077,

TheXman is correct - the dreaded "different datatype comparison" bug strikes again!

I cannot believe that no-one has run into this problem before - it seems very few people ever use this function with multiple range elements.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

Thanks guys,

I made had already made a work around by padding the number with the proceeding 0's.  I will also make the change in array.au3 file.

I have used _ArrayInsert before to insert a single items with no issues

I just happened to run across it because I have an array of data, that the user as the option to save to a file using _FileWriteFromArray, and i wanted to add some blank lines at certain locations  to make the output file easier to read.

so i had a loop that went through the data and if certain conditions were met to build a string like 3;5;7;9 ... etc so i could add the blank lines before writing it to a file.

I thought there for a minute I was going crazy, when it was telling me that 3;5;11 was not in order.  lol.  Glad it is something easily fixed in one line of code.

Thanks again guys.

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...