Sign in to follow this  
Followers 0
mark309

_arraysort

20 posts in this topic

Hey guys,

maybe it's a mondaymorning problem but i just can't get my array to sort on the second collumn. hope one of you sees the problem. I left out the parts i dont think are neccesary.

#include <Array.au3>
*********cut***********************
Else
    For $i = 1 To $var[0][0]
        
        if $var[$i][0] = @ComputerName then
        
            $getalvoordezepc=$var[$i][1]
            EndIf
            
            if $var[$i][1] > $getalvoordezepc then 
            $stopdezepcbij=($var[$i][1] -1)
                        EndIf
        
        
        
        
    ;MsgBox(4096, "", "Key: " & $var[$i][0] & @CRLF & "Value: " & $var[$i][1])
        
    
Next
msgbox(4096, "", $stopdezepcbij)
EndIf


_ArraySort($var,0,0,2,0)

_ArrayDisplay( $var, "blablabla" )

Thanks for youre help! :)

Share this post


Link to post
Share on other sites



_ArraySort($var,0,0,0,2)


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

_ArraySort($var,0,0,0,2)

Nope, that sorts on the first collumn and i am trying to sort on col1 (see image), changing the value to 3 or any other number gives an error:

Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

:)

post-32995-1212393987_thumb.jpg

Share this post


Link to post
Share on other sites

I don't have AutoIt on this system and I don't remembe the syntax for _Array Sort() but I do remember that the dimension to sort is the last parameter so try replacing the 2 with a 1.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Of course I never thought to look it up on my On-Line help page

_ArraySort(ByRef $avArray[, $iDescending = 0[, $iStart = 0[, $iEnd = 0[, $iSubItem = 0]]]])

So what you probably want is

_ArraySort($var,0,1,0,1)
Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Of course I never thought to look it up on my On-Line help page

So what you probably want is

_ArraySort($var,0,1,0,1)
Wow you are fast :)! but unfortunately also this one doesnt work, have been trying a lot of combo's but have not found a working one yet. This is the error with the option you gave me:

C:\Program Files\AutoIt3\Include\Array.au3 (666) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.: 
$t = $array[$i] 
$t = ^ ERROR

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Wow you are fast :)! but unfortunately also this one doesnt work, have been trying a lot of combo's but have not found a working one yet. This is the error with the option you gave me:

C:\Program Files\AutoIt3\Include\Array.au3 (666) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.: 
$t = $array[$i] 
$t = ^ ERROR
Since $t does not appear in the initial code I suspect that you have something else that is causing the error. Create the array then

_ArrayDisplay($array,"Unsorted")
_ArraySort($array, 0,1,0,1)
_ArrayDisplay($array,"Sorted")
Did the array get sorted? Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Since $t does not appear in the initial code I suspect that you have something else that is causing the error. Create the array then

_ArrayDisplay($array,"Unsorted")
_ArraySort($array, 0,1,0,1)
_ArrayDisplay($array,"Sorted")
Did the array get sorted?
The funny thing is, it already sorts on collumn0 (weird huh): but with the sorted one it still gives the error.

this is the .ini file:

[general]
computer1=20
computer2=3
computer3=1
computer5=7
computer4=10
computer6=8

If you look at the screenshot you can see it already sorted on the computername ??

post-32995-1212398065_thumb.jpg

Share this post


Link to post
Share on other sites

The funny thing is, it already sorts on collumn0 (weird huh): but with the sorted one it still gives the error.

this is the .ini file:

[general]
computer1=20
computer2=3
computer3=1
computer5=7
computer4=10
computer6=8

If you look at the screenshot you can see it already sorted on the computername ??

Thats beause the INI file is sorted so the array keeps the same order. You could try _IniSortSectionByValue() fom this UDF to sort the INI Section by value.

INI.au3


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Thats beause the INI file is sorted so the array keeps the same order. You could try _IniSortSectionByValue() fom this UDF to sort the INI Section by value.

INI.au3

Hmm, this seems to work but it only looks at the first number:

computer3=15
computer4=16
computer1=20
computer2=3
computer5=7
computer6=8

Share this post


Link to post
Share on other sites

Hmm, this seems to work but it only looks at the first number:

computer3=15
computer4=16
computer1=20
computer2=3
computer5=7
computer6=8
I just isolated your original problem. You are using an older version of Array.au3 and that is where the problem is.

I can make it work but You won't think it worked properly (it did)

Create a new script with this code and run it

;
#cs ----------------------------------------------------------------------------

 [general]
computer1=20
computer2=3
computer3=1
computer5=7
computer4=10
computer6=8
[dummy]
#ce ----------------------------------------------------------------------------

#include <array.au3>

$INI = @ScriptFullPath

$Array = INIReadSection($Ini, "general")

_ArrayDisplay($Array, "Unsorted")
_ArraySort($Array, 0,1,0,2,1)
_ArrayDisplay($Array,"Sorted")
;

You will see that it works

While you do that I will work out the rest of your problem


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Here is a display of whats happening to you as far as the sort order is concerned.

Change the code in the last example to this and then look at the array displays again.

;
#cs ----------------------------------------------------------------------------

 [general]
computer1=20
computer2=3
computer3=1
computer5=7
computer4=10
computer6=8
[dummy]
#ce ----------------------------------------------------------------------------

#include <array.au3>

$INI = @ScriptFullPath

$Array = INIReadSection($Ini, "general")
For $I = 1 to $Array[0][0]
$Array[$I][1] = StringFormat("%02d", $Array[$I][1])
Next
_ArrayDisplay($Array, "Unsorted")
_ArraySort($Array, 0,1,0,2,1)
For $I = 1 To $Array[0][0]
$Array[$I][1] = Int($Array[$I][1])
Next
_ArrayDisplay($Array,"Sorted")
;

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

That's intresting, I have v3.2.12.0 which is the latest, and the example above doesn't even work, as _ArraySort has 5 params, not 6.

Anyway, to OP - you could use this sorter http://www.autoitscript.com/forum/index.php?showtopic=63525

Note the post where I determined he is osing an older version? _ArraySort() Used to have 6 Parameters and that's what threw me off in the beginning.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Note the post where I determined he is osing an older version? _ArraySort() Used to have 6 Parameters and that's what threw me off in the beginning.

Sorry, I misread that part.

Anyway, if OP wants proper numerical sort, _ArraySort isn't the best function to use in this case (or any case for that matter). It simply doesn't support that, unless you loop through your array and Number() every element before passing the array to _ArraySort...


"be smart, drink your wine"

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

Sorry, I misread that part.

Anyway, if OP wants proper numerical sort, _ArraySort isn't the best function to use in this case (or any case for that matter). It simply doesn't support that, unless you loop through your array and Number() every element before passing the array to _ArraySort...

He's reading an Ini section with INIReadSEction() and he wants it sorted on the Value, not the Key. In the example he gave the Values are numeric and I continued using his example. That's what was messing with the sort order after it actually sorted. To solve that I used the StringFormat to set leading 0's so it would sort properly. I was thinking that my _INI_SortSectionByValue() would do it but the same numeric problem exists with that, not for long though. :) Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Sorry, I misread that part.

Anyway, if OP wants proper numerical sort, _ArraySort isn't the best function to use in this case (or any case for that matter). It simply doesn't support that, unless you loop through your array and Number() every element before passing the array to _ArraySort...

Hey guys, i used mr Siao's tip and i have it working now! You guys are great and saved my day! thumbs up!! :)

Share this post


Link to post
Share on other sites

Had the same issue, it would sort for anything but what I was trying to make it sort. Problem is that in order for _arraysort to work properly, the values to sort on have to be the same number of digits. So instead of

Row   Col 0   Col 1
[0]   6
[1]   computer 1    20
[2]   computer 2    3
[3]   computer 3    1
[4]   computer 4    10
[5]   computer 5    7
[6]   computer 6    8

it should be

Row   Col 0   Col 1
[0]   6
[1]   computer 1    20
[2]   computer 2    03
[3]   computer 3    01
[4]   computer 4    10
[5]   computer 5    07
[6]   computer 6    08

This could be done by adding something like

$amount_digits = 2
$len = stringlen($col_1)
if not ($len = $amount_digits) Then
    Do
        $col_1 = '0' & $col_1
        $len = stringlen($col_1)
    until $len = $amount_digits
EndIf
before writing $col_1 into the array. I know it's not foolproof (doesn't work on numbers that have more than the specified amount of digits), it's just a quick, dirty and ugly method if you know you'll never have numbers longer than the specified amount of digits.

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

Problem is that in order for _arraysort to work properly, the values to sort on have to be the same number of digits.

Not necessarily. And zero padding each number string is the worst way to go, unless maybe you are dealing with huge numbers that can't be expressed with numeric types properly. Edited by Siao

"be smart, drink your wine"

Share this post


Link to post
Share on other sites

Maybe so, but I find it strange that

Row   Col 0 Col 1
[0]   3     3
[1]   2     1013
[2]   3     1027
[3]   q     980
is wrongfully sorted like
Row   Col 0 Col 1
[0]   3     3
[1]   3     1027
[2]   q     980
[3]   2     1013

whereas

Row   Col 0 Col 1
[0]   3     3
[1]   2     1013
[2]   3     1027
[3]   q     0980
is sorted correctly to
Row   Col 0 Col 1
[0]   3     3
[1]   q     0980
[2]   2     1013
[3]   3     1027

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
Sign in to follow this  
Followers 0