Jump to content

Recommended Posts

Posted

I would like to sort array that contain number and dot as a key, but to get it sorted correctly, I need to delete the dot between the numbers.

eg:

data is 1.500, 500 and 11.500

Without delete the dot, using _ArraySort will result:

1.500, 11.500 and 500 which is understandable.

To get it sorted correctly, I delete the dot and convert it to number using number().

Code is below. It's working perfect. I wonder do we really need such a long code just for this? :P

#include <array.au3>

Local $aArray[3][4] = [['1','Michael','Mama ','1.500'], ['5','Susan','Xray ','500'], ['2', 'Adele', 'Telly ', '11.500']]

; remove the "." so it can sort correctly
For $a = 0 To UBound($aArray) - 1
    $aArray[$a][3] = Number(StringReplace($aArray[$a][3], ".", ""))
Next

_ArraySort($aArray, 0, 0, 0, 3)

; add the thousand separator
For $a = 0 To UBound($aArray) - 1
    $aArray[$a][3] = NumberCommas_with_dot($aArray[$a][3])
Next

_arraydisplay($aArray)

Func NumberCommas_with_dot($Number)
    ;Given a number such as 12345678 will return 12,345,678
    Dim $Result='', $Pos
    If StringIsDigit($Number) Then
        $Pos = StringLen($Number)-2
        While $Pos > -2
        If $Pos < 1 Then
            $Result = StringMid($Number, 1, $Pos + 2) & '.' & $Result
        Else
            $Result = StringMid($Number, $Pos, 3) & '.' & $Result
        EndIf
        $Pos = $Pos - 3
        WEnd
        $Result=StringLeft($Result, StringLen($Result) - 1)
    EndIf
Return $Result
EndFunc
Posted

Shortened code:

#include <array.au3>

Local $aArray[3][4] = [['1','Michael','Mama ','1.500'], ['5','Susan','Xray ','500'], ['2', 'Adele', 'Telly ', '11.500']]

; remove the "." so it can sort correctly
For $a = 0 To UBound($aArray) - 1
    $aArray[$a][3] = Number(StringReplace($aArray[$a][3], ".", ""))
Next

_ArraySort($aArray, 0, 0, 0, 3)

; add the thousand separator
For $a = 0 To UBound($aArray) - 1
    $aArray[$a][3] = StringRegExpReplace($aArray[$a][3], "(?<=\d)(\d\d\d)(?=(?:\d\d\d)*$)", ",$1")
Next

_arraydisplay($aArray)

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Posted

 

Shortened code:

#include <array.au3>

Local $aArray[3][4] = [['1','Michael','Mama ','1.500'], ['5','Susan','Xray ','500'], ['2', 'Adele', 'Telly ', '11.500']]

; remove the "." so it can sort correctly
For $a = 0 To UBound($aArray) - 1
    $aArray[$a][3] = Number(StringReplace($aArray[$a][3], ".", ""))
Next

_ArraySort($aArray, 0, 0, 0, 3)

; add the thousand separator
For $a = 0 To UBound($aArray) - 1
    $aArray[$a][3] = StringRegExpReplace($aArray[$a][3], "(?<=\d)(\d\d\d)(?=(?:\d\d\d)*$)", ",$1")
Next

_arraydisplay($aArray)

 

Wow, again the power of RegExp :)

Thanks, jchd :)

Anyone? :P

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
  • Recently Browsing   0 members

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