Jump to content

New problem with Arrays - ignore my other topic


 Share

Recommended Posts

I have an array set up like this.

Dim $targets[4][100]

On $targets[4][$i] - dates are stored.

I want to sort the array so that the dates are sorted from the earliest to the latest. I have no idea how I would do this. Some of them are on the same date but with different times also....

Any ideas?

I tried _DateToDayValue but it did not seem to work very well.

Link to comment
Share on other sites

I have an array set up like this.

Dim $targets[4][100]

On $targets[4][$i] - dates are stored.

I want to sort the array so that the dates are sorted from the earliest to the latest. I have no idea how I would do this. Some of them are on the same date but with different times also....

Any ideas?

I tried _DateToDayValue but it did not seem to work very well.

mm, how does the date look like? like: 09/06/05 to 09/06/07 ?

you could try: _ArrayMinIndex() and then remove the result from your array, and place the result to a new array,

then u scan it again (so it will get the second lowest value) and do it the same,

ofcourse u need it to place this in a loop to constantly scan the array and put it into a new array,

this is only a theory, i'm at someone's so i can't help you with coding :">

good luck,

-me

Link to comment
Share on other sites

  • Moderators

Array To String

String + StringRegExp

StringRegExp Back To Array

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

Here's a rough example of what I'm talking about (Your arrays had better be right and 0 based):

#include <array.au3>
Dim $a[2][12]
$a[1][0] = '01/30/2007'
$a[1][1] = '01/12/2007'
$a[1][2] = '01/16/2007'
$a[1][3] = '01/14/2007'
$a[1][4] = '01/22/2007'
$a[1][5] = '01/18/2007'
$a[1][6] = '01/13/2007'
$a[1][7] = '01/26/2007'
$a[1][8] = '01/03/2007'
$a[1][9] = '01/08/2007'
$a[1][10] = '01/24/2007'
$a[1][11] = '01/06/2007'

_ArraySortDate2Dim($a, 1, 2)
_ArrayDisplay2D($a)

Func _ArraySortDate2Dim(ByRef $avArray, $iElementNum = 0, $iDimensionsUbound = 1)
    UBound($avArray, 2)
    If @error Then Return SetError(1, 0, '')
    Local $sString = ''
    If $iDimensionsUbound = 1 Then
        For $iCC = 0 To UBound($avArray, 1) - 1
            $sString &= StringReplace($avArray[$iCC][$iElementNum], '/', '') & Chr(1)
        Next
    Else
        For $iCC = 0 To UBound($avArray, 2) - 1
            $sString &= StringReplace($avArray[$iElementNum][$iCC], '/', '') & Chr(1)
        Next
    EndIf
    Local $aArray = StringRegExp($sString, '(\d{8})+', 3)
    If IsArray($aArray) = 0 Then Return SetError(2, 0, '')
    _ArraySortNum($aArray, 1, 0)
    If $iDimensionsUbound = 1 Then
        For $iCC = 0 To UBound($avArray, 1) - 1
            $avArray[$iCC][$iElementNum] = StringMid($aArray[$iCC], 1, 2) & '/' & _
                StringMid($aArray[$iCC], 3, 2) & '/' & _
                StringMid($aArray[$iCC], 5, 4)
        Next
    Else
        For $iCC = 0 To UBound($avArray, 2) - 1
            $avArray[$iElementNum][$iCC] = StringMid($aArray[$iCC], 1, 2) & '/' & _
                StringMid($aArray[$iCC], 3, 2) & '/' & _
                StringMid($aArray[$iCC], 5, 4)
        Next
    EndIf
    Return 1
EndFunc
    
Func _ArraySortNum(ByRef $nArray, $Ascending = 0, $Start = 1)
    If $Ascending <> 0 And $Ascending <> 1 Then Return SetError(1)
    For $i = $Start To UBound($nArray) - 2
        Local $SE = $i
        If $Ascending = 0 Then
            For $x = $i To UBound($nArray) - 1
                If Number($nArray[$SE]) < Number($nArray[$x]) Then $SE = $x
            Next
        Else
            For $x = $i To UBound($nArray) - 1
                If Number($nArray[$SE]) > Number($nArray[$x]) Then $SE = $x
            Next
        EndIf
        Local $HLD = $nArray[$i]
        $nArray[$i] = $nArray[$SE]
        $nArray[$SE] = $HLD
    Next
EndFunc

Func _ArrayDisplay2D($aArray, $sTitle = 'Array Display 2Dim', $iBase = 1, $sToConsole = 0)
    If Not IsArray($aArray) Then Return SetError(1, 0, 0)
    Local $sHold = 'Dimension 1 Has:  ' & UBound($aArray, 1) -1 & ' Element(s)' & @LF & _
            'Dimension 2 Has:  ' & UBound($aArray, 2) - 1 & ' Element(s)' & @LF & @LF
    For $iCC = $iBase To UBound($aArray, 1) - 1
        For $xCC = 0 To UBound($aArray, 2) - 1
            $sHold &= '[' & $iCC & '][' & $xCC & ']  = ' & $aArray[$iCC][$xCC] & @LF
        Next
    Next
    If $sToConsole Then Return ConsoleWrite(@LF & $sHold)
    Return MsgBox(262144, $sTitle, StringTrimRight($sHold, 1))
EndFunc

I'm almost curious if we could just use StringRegExpReplace with back referencing to get an easier output.

Edit:

Forgot ByRef (goes to show how much I tested it :) )

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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