gcue

sort array code alphabetically

16 posts in this topic

#1 ·  Posted (edited)

hello world

i am trying to sort array code alphabetically by a column selected by the user (both multiple or single column arrays)... below is a snippet example of an array code.  I am trying to figure out the best way to get about it but not sure which is the best way

#include <Array.au3>
$msg_normal = 262144

#include <File.au3>

$string = 'Local $reg_changes[4][8]' & @CRLF
$string &= '' & @CRLF
$string &= '$reg_changes[0][0] = "NON_ADMIN"' & @CRLF
$string &= '$reg_changes[0][1] = "Windows - Default Restart Button"' & @CRLF
$string &= '$reg_changes[0][2] = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced"' & @CRLF
$string &= '$reg_changes[0][3] = "Start_PowerButtonAction"' & @CRLF
$string &= '$reg_changes[0][4] = "REG_DWORD"' & @CRLF
$string &= '$reg_changes[0][5] = "4"' & @CRLF
$string &= '$reg_changes[0][6] = "Requires Restart To Take Effect"' & @CRLF
$string &= '$reg_changes[0][7] = ""' & @CRLF
$string &= '' & @CRLF
$string &= '$reg_changes[1][0] = "NON_ADMIN"' & @CRLF
$string &= '$reg_changes[1][1] = "SnagIT Editor - Run in Background"' & @CRLF
$string &= '$reg_changes[1][2] = "HKEY_CURRENT_USER\Software\TechSmith\SnagIt\11"' & @CRLF
$string &= '$reg_changes[1][3] = "AlwaysKeepEditorOpen"' & @CRLF
$string &= '$reg_changes[1][4] = "REG_DWORD"' & @CRLF
$string &= '$reg_changes[1][5] = "1"' & @CRLF
$string &= '$reg_changes[1][6] = ""' & @CRLF
$string &= '$reg_changes[1][7] = "snagit.exe|snagiteditor.exe"' & @CRLF
$string &= '' & @CRLF
$string &= '$reg_changes[2][0] = "NON_ADMIN"' & @CRLF
$string &= '$reg_changes[2][1] = "Windows - Disable Auto Window Maximizing"' & @CRLF
$string &= '$reg_changes[2][2] = "HKEY_CURRENT_USER\Control Panel\Desktop"' & @CRLF
$string &= '$reg_changes[2][3] = "WindowArrangementActive"' & @CRLF
$string &= '$reg_changes[2][4] = "REG_SZ"' & @CRLF
$string &= '$reg_changes[2][5] = "0"' & @CRLF
$string &= '$reg_changes[2][6] = "Requires Restart To Take Effect"' & @CRLF
$string &= '$reg_changes[2][7] = ""' & @CRLF
$string &= '' & @CRLF
$string &= '$reg_changes[3][0] = "NON_ADMIN"' & @CRLF
$string &= '$reg_changes[3][1] = "Mouse Cursor - Display Pointer Trails"' & @CRLF
$string &= '$reg_changes[3][2] = "HKEY_CURRENT_USER\Control Panel\Cursors"' & @CRLF
$string &= '$reg_changes[3][3] = "Scheme Source"' & @CRLF
$string &= '$reg_changes[3][4] = "REG_DWORD"' & @CRLF
$string &= '$reg_changes[3][5] = "0"' & @CRLF
$string &= '$reg_changes[3][6] = "Requires Restart To Take Effect"' & @CRLF
$string &= '$reg_changes[3][7] = ""' & @CRLF


$variable = "$reg_changes"
$number_of_records = 4
$number_of_columns = 8

$result = Alphabetize($string, $variable, $number_of_records, $number_of_columns)

ConsoleWrite($result & @CRLF)

Func Alphabetize($string, $variable, $number_of_records, $number_of_columns)

    $sort_by_column = InputBox("Sort by which column #?", " ", "", " M", 320, 120)

    If @error Then
        Return
    EndIf

    $temp_file = _TempFile(@TempDir, "~", ".txt")

    FileWrite($temp_file, $string)

    $line_count = _FileCountLines($temp_file)

    $result = ""

    $current_record = 0
    $current_column = 0

    For $x = 1 To $line_count
        $line = FileReadLine($temp_file, $x)

        $data_array = StringSplit($line, "=")

        If UBound($data_array) - 1 = 2 Then
            $record_data = StringStripWS($data_array[2], 3)
;~          Debug($record_data)
            $result &= $variable & '[' & $current_record & '] = "' & $line & '"' & @CRLF
        Else
            $result &= $line & @crlf
        EndIf

        $current_record += 1
    Next

    FileDelete($temp_file)

    Return $result

EndFunc   ;==>Alphabetize


Func Debug($variable1 = "", $variable2 = "", $variable3 = "", $variable4 = "")

;~  #include <array.au3>
;~  $msg_normal = 0

    If IsArray($variable1) Then
        _ArrayDisplay($variable1)
    Else
        If $variable2 <> "" Then
            $variable1 &= @CRLF & $variable2
        EndIf

        If $variable3 <> "" Then
            $variable1 &= @CRLF & $variable3
        EndIf

        If $variable4 <> "" Then
            $variable1 &= @CRLF & $variable4
        EndIf

        ClipPut($variable1)
        MsgBox($msg_normal, "Debug", $variable1)
    EndIf

EndFunc   ;==>Debug

any help is greatly appreciated!

Edited by gcue

Share this post


Link to post
Share on other sites



#4 ·  Posted (edited)

junkew/AutoBert

the string is not technically an array yet.. just the array code.  so neither approaches i dont think are possible

Edited by gcue

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

This makes no sense to me. _ArraySort() sorts alphabetically (apart from numbers) on any column, so what's the problem?

Edited by czardas

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

hey czardas! 

its array code - not an actual array quite yet.. (i am working on a script that modifies user entered array code)

Edited by gcue

Share this post


Link to post
Share on other sites

Better you create a hardcoded array. 

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Well then sort it as soon as it becomes an array. If you then want to hard code the declaration string of the sorted array, you could try this:

 

Edited by czardas
1 person likes this

Share this post


Link to post
Share on other sites

@GCUE: Not sure what you try to do but why first in a string then in a file then read it back then split it. You seem to be making it very complex. Whats the full solution you try to accomplish. At first sight it looks that there are better ways of doing it.

1 person likes this

Share this post


Link to post
Share on other sites

@Czardas: The UDF you linked uses a other formated string.

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Got it working... here's a working example of what i have.  I just need quotes around each result.  Problem is if I add quotes, the secondary script doesn't see it as a variable

#include <Array.au3>
$msg_normal = 262144

#include <File.au3>

$string = 'Local $reg_changes[4][8]' & @CRLF
$string &= '' & @CRLF
$string &= '$reg_changes[0][0] = "NON_ADMIN"' & @CRLF
$string &= '$reg_changes[0][1] = "Windows - Default Restart Button"' & @CRLF
$string &= '$reg_changes[0][2] = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced"' & @CRLF
$string &= '$reg_changes[0][3] = "Start_PowerButtonAction"' & @CRLF
$string &= '$reg_changes[0][4] = "REG_DWORD"' & @CRLF
$string &= '$reg_changes[0][5] = "4"' & @CRLF
$string &= '$reg_changes[0][6] = "Requires Restart To Take Effect"' & @CRLF
$string &= '$reg_changes[0][7] = ""' & @CRLF
$string &= '' & @CRLF
$string &= '$reg_changes[1][0] = "NON_ADMIN"' & @CRLF
$string &= '$reg_changes[1][1] = "SnagIT Editor - Run in Background"' & @CRLF
$string &= '$reg_changes[1][2] = "HKEY_CURRENT_USER\Software\TechSmith\SnagIt\11"' & @CRLF
$string &= '$reg_changes[1][3] = "AlwaysKeepEditorOpen"' & @CRLF
$string &= '$reg_changes[1][4] = "REG_DWORD"' & @CRLF
$string &= '$reg_changes[1][5] = "1"' & @CRLF
$string &= '$reg_changes[1][6] = ""' & @CRLF
$string &= '$reg_changes[1][7] = "snagit.exe|snagiteditor.exe"' & @CRLF
$string &= '' & @CRLF
$string &= '$reg_changes[2][0] = "NON_ADMIN"' & @CRLF
$string &= '$reg_changes[2][1] = "Windows - Disable Auto Window Maximizing"' & @CRLF
$string &= '$reg_changes[2][2] = "HKEY_CURRENT_USER\Control Panel\Desktop"' & @CRLF
$string &= '$reg_changes[2][3] = "WindowArrangementActive"' & @CRLF
$string &= '$reg_changes[2][4] = "REG_SZ"' & @CRLF
$string &= '$reg_changes[2][5] = "0"' & @CRLF
$string &= '$reg_changes[2][6] = "Requires Restart To Take Effect"' & @CRLF
$string &= '$reg_changes[2][7] = ""' & @CRLF
$string &= '' & @CRLF
$string &= '$reg_changes[3][0] = "NON_ADMIN"' & @CRLF
$string &= '$reg_changes[3][1] = "Mouse Cursor - Display Pointer Trails"' & @CRLF
$string &= '$reg_changes[3][2] = "HKEY_CURRENT_USER\Control Panel\Cursors"' & @CRLF
$string &= '$reg_changes[3][3] = "Scheme Source"' & @CRLF
$string &= '$reg_changes[3][4] = "REG_DWORD"' & @CRLF
$string &= '$reg_changes[3][5] = "0"' & @CRLF
$string &= '$reg_changes[3][6] = "Requires Restart To Take Effect"' & @CRLF
$string &= '$reg_changes[3][7] = ""' & @CRLF

$variable = "$reg_changes"
$number_of_records = 4
$number_of_columns = 8

$result = Alphabetize($string, $variable, $number_of_records, $number_of_columns)

ConsoleWrite($result & @CRLF)

Func Alphabetize($string, $variable, $number_of_records, $number_of_columns)

    $sort_by_column = InputBox("Sort by which column #?", " ", "", " M", 320, 120)

    If @error Then
        Return
    EndIf

    $temp_input_file = _TempFile(@TempDir, "~", ".au3")
    $temp_exe_file = @ScriptDir & "\array_script.exe"
    $temp_output_file = _TempFile(@TempDir, "~", ".txt")

    $string &= "" & @CRLF
    $string &= "_ArraySort(" & $variable & ",0,0,0," & $sort_by_column & ")" & @CRLF
;~  $string &= "_ArrayDisplay(" & $variable & ") & @CRLF" & @CRLF
    $string &= "" & @CRLF

    For $x = 0 To $number_of_records - 1
        For $y = 0 To $number_of_columns - 1
            $string &= 'FileWriteLine("' & $temp_output_file & '","' & $variable & '[' & $x & '][' & $y & '] = " & ' & $variable & '[' & $x & '][' & $y & '])' & @CRLF ;**NEED QUOTES AROUND THIS RESULT
        Next
        $string &= 'FileWriteLine("' & $temp_output_file & '", @CRLF)' & @CRLF
    Next

    $string &= "" & @CRLF
    $string &= '$data = FileRead("' & $temp_output_file & '")' & @CRLF
    $string &= "" & @CRLF
    $string &= "ClipPut($data)" & @CRLF

    FileWrite($temp_input_file, "#include <Array.au3>" & @CRLF & @CRLF)
    FileWrite($temp_input_file, $string)

    ShellExecute($temp_input_file)

    FileWrite($temp_output_file, "Local $reg_changes[4][8]" & @CRLF & @CRLF)

    $autoit_dir = "C:\Program Files (x86)\AutoIt v3.3.12.0\Aut2Exe"
    RunWait($autoit_dir & "\Aut2exe.exe /in " & $temp_input_file & " /out " & $temp_exe_file)
    RunWait($temp_exe_file)

    $results = ClipGet()

;~  ShellExecute($temp_output_file)

    FileDelete($temp_input_file)
    FileDelete($temp_output_file)
    FileDelete($temp_exe_file)

    Return $results

EndFunc   ;==>Alphabetize


Func Debug($variable1 = "", $variable2 = "", $variable3 = "", $variable4 = "")

;~  #include <array.au3>
;~  $msg_normal = 0

    If IsArray($variable1) Then
        _ArrayDisplay($variable1)
    Else
        If $variable2 <> "" Then
            $variable1 &= @CRLF & $variable2
        EndIf

        If $variable3 <> "" Then
            $variable1 &= @CRLF & $variable3
        EndIf

        If $variable4 <> "" Then
            $variable1 &= @CRLF & $variable4
        EndIf

        ClipPut($variable1)
        MsgBox($msg_normal, "Debug", $variable1)
    EndIf

EndFunc   ;==>Debug

 

Edited by gcue

Share this post


Link to post
Share on other sites

Yes _ArrayAssign is a func of your UDF but the expected string is other format than OP's format, i tested with this script:

#include <ArrayMultiDim.au3>
#include <String.au3>
$msg_normal = 262144

#include <File.au3>

$string = 'Local $reg_changes[4][8]' & @CRLF
$string &= '' & @CRLF
$string &= '$reg_changes[0][0] = "NON_ADMIN"' & @CRLF
$string &= '$reg_changes[0][1] = "Windows - Default Restart Button"' & @CRLF
$string &= '$reg_changes[0][2] = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced"' & @CRLF
$string &= '$reg_changes[0][3] = "Start_PowerButtonAction"' & @CRLF
$string &= '$reg_changes[0][4] = "REG_DWORD"' & @CRLF
$string &= '$reg_changes[0][5] = "4"' & @CRLF
$string &= '$reg_changes[0][6] = "Requires Restart To Take Effect"' & @CRLF
$string &= '$reg_changes[0][7] = ""' & @CRLF
$string &= '' & @CRLF
$string &= '$reg_changes[1][0] = "NON_ADMIN"' & @CRLF
$string &= '$reg_changes[1][1] = "SnagIT Editor - Run in Background"' & @CRLF
$string &= '$reg_changes[1][2] = "HKEY_CURRENT_USER\Software\TechSmith\SnagIt\11"' & @CRLF
$string &= '$reg_changes[1][3] = "AlwaysKeepEditorOpen"' & @CRLF
$string &= '$reg_changes[1][4] = "REG_DWORD"' & @CRLF
$string &= '$reg_changes[1][5] = "1"' & @CRLF
$string &= '$reg_changes[1][6] = ""' & @CRLF
$string &= '$reg_changes[1][7] = "snagit.exe|snagiteditor.exe"' & @CRLF
$string &= '' & @CRLF
$string &= '$reg_changes[2][0] = "NON_ADMIN"' & @CRLF
$string &= '$reg_changes[2][1] = "Windows - Disable Auto Window Maximizing"' & @CRLF
$string &= '$reg_changes[2][2] = "HKEY_CURRENT_USER\Control Panel\Desktop"' & @CRLF
$string &= '$reg_changes[2][3] = "WindowArrangementActive"' & @CRLF
$string &= '$reg_changes[2][4] = "REG_SZ"' & @CRLF
$string &= '$reg_changes[2][5] = "0"' & @CRLF
$string &= '$reg_changes[2][6] = "Requires Restart To Take Effect"' & @CRLF
$string &= '$reg_changes[2][7] = ""' & @CRLF
$string &= '' & @CRLF
$string &= '$reg_changes[3][0] = "NON_ADMIN"' & @CRLF
$string &= '$reg_changes[3][1] = "Mouse Cursor - Display Pointer Trails"' & @CRLF
$string &= '$reg_changes[3][2] = "HKEY_CURRENT_USER\Control Panel\Cursors"' & @CRLF
$string &= '$reg_changes[3][3] = "Scheme Source"' & @CRLF
$string &= '$reg_changes[3][4] = "REG_DWORD"' & @CRLF
$string &= '$reg_changes[3][5] = "0"' & @CRLF
$string &= '$reg_changes[3][6] = "Requires Restart To Take Effect"' & @CRLF
$string &= '$reg_changes[3][7] = ""' & @CRLF

Local $array[0]
_ArrayAssign($array,$string)
_ArrayDisplay($array)

your UDF is good, specially _ArrayDeclareFromString and _ArrayToDeclarationString which i often use.

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

@gcue I tried to run your code but got errors.

Below is a nice snippet from @spudw2k.
source: https://www.autoitscript.com/forum/topic/180467-arrayworkshop/?do=findComment&comment=1296232

With a little modification it should all come together in the end :), although @junkew is probably right in saying there may be easier ways to do what you want.

Local $aData = [['Header1','Header2','Header3'],['data1','data2','data3'],['data4','data5','data6']]
; sort the array
_DynEnumArray($aData)

Func _DynEnumArray($arr) ; by spudw2k
    Local $arrDims[1][2]=[["ElementIdx","ElementCount"]]   ;Create an Array to Track Dimensions and Elements
    For $x = 1 to UBound($arr,0)
        ReDim $arrDims[$x+1][2]
        $arrDims[$x][0]=0
        $arrDims[$x][1]=UBound($arr,$x)
    Next
    Do   ;Loop Through Array Elements
        $var = "$arr"
        For $x = 1 to UBound($arrDims)-1
            $var &= "[" & $arrDims[$x][0] & "]"
        Next
        
        ; modified next line [czardas]
        ConsoleWrite($var & ' = "' & Execute($var) & '"' & @CRLF)  ;Output Subscript and Value

        $arrDims[UBound($arrDims)-1][0] += 1   ;Increment Last Dimension Element
        For $y = UBound($arrDims)-2 To 1 Step -1   ;Increment Dimension Element
            If $arrDims[$y+1][0] = $arrDims[$y+1][1] Then
                $arrDims[$y+1][0]=0
                $arrDims[$y][0]+=1
            EndIf
        Next

    Until $arrDims[1][0]=$arrDims[1][1]
    Return ConsoleWrite(@CRLF)
EndFunc

A little more modification (to the line I modified) should give the desired format. I suggest you play around with it.

Edited by czardas

Share this post


Link to post
Share on other sites

ill check it out

thanks!

Share this post


Link to post
Share on other sites

looks like it's going to involve to much modification to the rest of the script to put the arrays in that format.  i think i'm going to go with what i have which is working  (previous post). =)

thanks anyway!

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