Jump to content

_arraysort Adaptation


Recommended Posts

I'm trying to figure out how to get _Arraysort to work for this array... basically I read a line in a from a file and use StringSplit... which makes it into a 3 dimension Array. What parameters should I dictate to _ArraySort to sort the array by the third dimension(the percent)?

Edited by exodius
Link to comment
Share on other sites

  • Developers

I'm trying to figure out how to get _Arraysort to work for this array... basically I read a line in a from a file and use StringSplit... which makes it into a 3 dimension Array. What parameters should I dictate to _ArraySort to sort the array by the third dimension(the percent)?

Show some code and so we can see what is wrong with it...

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

I'm trying to figure out how to get _Arraysort to work for this array... basically I read a line in a from a file and use StringSplit... which makes it into a 3 dimension Array. What parameters should I dictate to _ArraySort to sort the array by the third dimension(the percent)?

Note that StringSplit does not create a 3D array... it will create a 1D array, which may have three cells... :)

From the help file on StringSplit:

Return Value

Returns an array, the first element ($array[0]) contains the number of strings returned, the remaining elements ($array[1], $array[2], etc.) contain the delimited strings.

If no delimiters were found @error is set to 1, the count is 1 ($array[0]) and the full string is returned ($array[1]).

:(
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

All right, it was after some obvious thought that I came to realize that I can't ArraySort a StringSplit array because that array is only as large as the split... So here's my real situation, I read in a file using _FileReadToArray much like the one below. There are three different things per line, the type, the number of that type, and the percentage of the total. I need to sort the array by the third item, the percent, any ideas on how?

Abuse,1,3%
Application,6,20%
E-Mail,4,13%
Net ID,4,13%
Network,5,17%
Other,8,27%
Phone,1,3%
Web (general),1,3%
Total,30
Link to comment
Share on other sites

this is close

** tested ok

#include <Array.au3>
#include <file.au3>

Dim $aRecords
If Not _FileReadToArray("example.txt",$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf

Dim $info[$aRecords[0]] 

For $x = 1 to $aRecords[0] -2
    $result = StringSplit($aRecords[$x], ",")
    $info[$x] = $result[3]
Next

_ArraySort($info, 0, 1)
_ArrayDisplay($info, "Display")

8)

NEWHeader1.png

Link to comment
Share on other sites

this is close

** tested ok

#include <Array.au3>
#include <file.au3>

Dim $aRecords
If Not _FileReadToArray("example.txt",$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf

Dim $info[$aRecords[0]] 

For $x = 1 to $aRecords[0] -2
    $result = StringSplit($aRecords[$x], ",")
    $info[$x] = $result[3]
Next

_ArraySort($info, 0, 1)
_ArrayDisplay($info, "Display")

8)

I've thought about doing it that way, the problem I always run into is it'd be easy to sort the percentages, but then the rest of the line would lose it's association to the percent... :)
Link to comment
Share on other sites

  • Moderators

I've thought about doing it that way, the problem I always run into is it'd be easy to sort the percentages, but then the rest of the line would lose it's association to the percent... :)

Have you tried StringRegExp('', '', 3) on each line to return them in an array? With some manipulation, I don't see this being an "impossible" venture, but could very well be a tedious one.

Alot of guys on here (neogia, Simucal, etc... ) enjoy playing around with StringRegExp(), if you uploaded an actual text file, and put the desired output you would like, I'm sure that someone would give it a go to help :(.

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

I've thought about doing it that way, the problem I always run into is it'd be easy to sort the percentages, but then the rest of the line would lose it's association to the percent... :)

As _arraysort has this remark: "This sort functions can handle 1 or 2 dimension arrays.", perhaps you can do something like this pseudocode:

1. Read line from file
2. Replace first comma with a "~" tilde.
3. Split the line at the remaining (second) comma and add it to an (Nx2) array
4. Sort the Nx2 array
5. Split the first array value at the tilde and do what you like with the data.
Ugly, but may work....
...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

This is untested cuz im not on a machine w/autoit but should work.

#include <Array.au3>
#include <file.au3>

Dim $aRecords
If Not _FileReadToArray("example.txt",$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf

Dim $pcnt[$aRecords[0]]
Dim $info[$aRecords[0] * 3]

For $iter = 1 to $aRecords[0] -2
    $result = StringSplit($aRecords[$x], ",")
    $info[ Int( $iter / 3 ) + 1 ] = $result[1]
    $info[ Int( $iter / 3 ) + 2 ] = $result[2]
    $info[ Int( $iter / 3 ) + 3 ] = $result[3]
    $pcnt[$x] = $result[3]
Next

_ArraySort($pcnt, 0, 1)

Dim $display[$aRecords[0]]

For $x = 0 to $aRecords[0] - 1
    $key = _ArrayBinarySearch( $info, $pcnt[ $x + 1 ] )
    $display[ $x ] = $info[ $key - 2 ] & " - " & $info[ $key - 1 ] & " using " & $info[ $x ] & "%"
Next

_ArrayDisplay($display, "Display")

This keeps an array of all the records as they are read in from the file. Then after the percents are sorted it traverses the sorted % array and finds each corresponding % value in the all inclusive info array. Then it forms that content from the info array into something understandable and presentable to the user and places all that in the display array. Let me know if this works....sry i couldnt test it first.

_____________________________________________________"some people live for the rules, I live for exceptions"Wallpaper Changer - Easily Change Your Windows Wallpaper

Link to comment
Share on other sites

Alot of guys on here (neogia, Simucal, etc... ) enjoy playing around with StringRegExp()...

I hear they also enjoy root canals and Ashlee Simpson records... :)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

  • Developers

this code should have most of the stuff you need:

#include <Array.au3>
#include <file.au3>
$InputFile = "test.txt"
Dim $reccount = _FileCountLines($InputFile)
Dim $aRecords[$reccount][3]
Dim $result
$reccount = 0

$file = FileOpen("test.txt", 0)
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    $result = StringSplit($line, ",")
    For $y = 1 To $result[0]
        $aRecords[$reccount][$y-1] = $result[$y]
    ; make the persent column numeric
        If $y = 3 Then 
            $aRecords[$reccount][$y-1] = Number(StringReplace($result[$y],"%",""))
        EndIf
    Next
    $reccount = $reccount + 1
Wend

FileClose($file)

_ArraySort($aRecords, 0, 0, 0, 3, 2)

For $x = 0 To UBound($aRecords) -1
    ConsoleWrite('Record= ' & $x & " Fld1:" & $aRecords[$x][0] )
    ConsoleWrite(" Fld2:" & $aRecords[$x][1] )
    ConsoleWrite(" Fld3:" & $aRecords[$x][2] & @LF )
Next

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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