Jump to content

_FileReadToArray declares all information as char?


Recommended Posts

Hello.

I'm just transfering some of my old Pascal programs into Autoit (Want to make some pretty GUI's)

And allot of them have something to do with importing/exporting arrays.

For example, Bubble sorting (Yeah I know Shells and Hoars algorithm is better, just testing)

Func Sort()
    _FileReadToArray(@ScriptDir & "\test.txt", $A)
    $Loop = $A[0]
$I=2; {Шаг поиска}
While $I<=$Loop;{Пока шаг поиска меньше кол-ва элементов массива}
$J=$Loop; {Максимальный номер массива}
While $J>=$I;{Пока номер элемента больше шага}
    If $A[$J-1]>$A[$J] then;{И Предыдущий элемент больше следующего}
    $X=$A[$J-1]; {Помещаем элемент в X}
    $A[$J-1]=$A[$J]; {Меняем местами элементы}
    $A[$J]=$X;
    EndIf; {/If}
$J=$J-1; {Исключаем отсортированный элемент из поиска}
    WEnd; {/While}
    $I=$I+1; {Увеличиваем шаг поиска}
WEnd; {/While}
_FileWriteFromArray(@ScriptDir & "\Sorted.txt", $A, 1)
EndFunc

This

4
3
4
3
4
3
4
34
2
2
3
2
3
4
2
3
4
23
4
3
2
3
4
3
2

Gets sorted into this:

2
2
2
2
2
2
23
3
3
3
3
3
3
3
3
3
34
4
4
4
4
4
4
4
4

I tried this with lots of numbers, and the only numbers it sorts correctly are 0-9 numbers.

So I figured FileReadToArray stores all information as strings or chars. Is it possible to store it as Integer?

I figured this was asked before, but couldn't find it while searching....

Edited by Qousio
Link to comment
Share on other sites

  • Moderators

Func _ArraySortNum(ByRef $n_array, $i_descending = 0, $i_start = 1)
    For $i_count = $i_start To UBound($n_array) - 2
        Local $i_se = $i_count
        If $i_descending Then
            For $x_count = $i_count To UBound($n_array) - 1
                If Number($n_array[$i_se]) < Number($n_array[$x_count]) Then $i_se = $x_count
            Next
        Else
            For $x_count = $i_count To UBound($n_array) - 1
                If Number($n_array[$i_se]) > Number($n_array[$x_count]) Then $i_se = $x_count
            Next
        EndIf
        Local $i_hld = $n_array[$i_count]
        $n_array[$i_count] = $n_array[$i_se]
        $n_array[$i_se] = $i_hld
    Next
EndFunc

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

Ты можешь использовать функцию Int().

Спасибо за совет! Всё работает :party: Я думал в Аутоит нельзя обьявлять типы.

Func _ArraySortNum(ByRef $n_array, $i_descending = 0, $i_start = 1)
    For $i_count = $i_start To UBound($n_array) - 2
        Local $i_se = $i_count
        If $i_descending Then
            For $x_count = $i_count To UBound($n_array) - 1
                If Number($n_array[$i_se]) < Number($n_array[$x_count]) Then $i_se = $x_count
            Next
        Else
            For $x_count = $i_count To UBound($n_array) - 1
                If Number($n_array[$i_se]) > Number($n_array[$x_count]) Then $i_se = $x_count
            Next
        EndIf
        Local $i_hld = $n_array[$i_count]
        $n_array[$i_count] = $n_array[$i_se]
        $n_array[$i_se] = $i_hld
    Next
EndFunc
Excuse me, but that doesnt have anything to do with FileReadToArray :)

Anyhow, Yashied's advice solved my problem.

Thank you!

Link to comment
Share on other sites

  • Moderators

Excuse me, but that doesnt have anything to do with FileReadToArray :)

No kidding, I was showing you how to sort with numbers.

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

Func _ArraySortNum(ByRef $n_array, $i_descending = 0, $i_start = 1)
    For $i_count = $i_start To UBound($n_array) - 2
        Local $i_se = $i_count
        If $i_descending Then
            For $x_count = $i_count To UBound($n_array) - 1
                If Number($n_array[$i_se]) < Number($n_array[$x_count]) Then $i_se = $x_count
            Next
        Else
            For $x_count = $i_count To UBound($n_array) - 1
                If Number($n_array[$i_se]) > Number($n_array[$x_count]) Then $i_se = $x_count
            Next
        EndIf
        Local $i_hld = $n_array[$i_count]
        $n_array[$i_count] = $n_array[$i_se]
        $n_array[$i_se] = $i_hld
    Next
EndFunc
Or use this function before sorting. Perhaps it will be faster.

Func _ArrayConvert(ByRef $Array)
    For $i = 1 To $Array[0]
        $Array[$i] = Int($Array[$i])
    Next
EndFunc; _ArrayConvert
Link to comment
Share on other sites

Спасибо за совет! Всё работает :party: Я думал в Аутоит нельзя обьявлять типы.

Excuse me, but that doesnt have anything to do with FileReadToArray :)

Anyhow, Yashied's advice solved my problem.

Thank you!

Excuse me but if you had checked the function SmOke_N gave you then you might have noticed that passing the array created by _FileReadToArray() to the function would have sorted the origional array as integers.

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!"

Link to comment
Share on other sites

No kidding, I was showing you how to sort with numbers.

Oh, thanks!

Its just 2 am and my brain is completely focused on other things.... Thanks again.

Edited by Qousio
Link to comment
Share on other sites

Func _ArraySortNum(ByRef $n_array, $i_descending = 0, $i_start = 1)
    For $i_count = $i_start To UBound($n_array) - 2
        Local $i_se = $i_count
        If $i_descending Then
            For $x_count = $i_count To UBound($n_array) - 1
                If Number($n_array[$i_se]) < Number($n_array[$x_count]) Then $i_se = $x_count
            Next
        Else
            For $x_count = $i_count To UBound($n_array) - 1
                If Number($n_array[$i_se]) > Number($n_array[$x_count]) Then $i_se = $x_count
            Next
        EndIf
        Local $i_hld = $n_array[$i_count]
        $n_array[$i_count] = $n_array[$i_se]
        $n_array[$i_se] = $i_hld
    Next
EndFunc
Nice! Could be an UDF for File.au3 include; _FileSortNumLines (or something :))...

#include <File.au3>

_FileSortNumLines(@ScriptDir & "\Sort.txt")

Func _FileSortNumLines($sSrc_File, $sDest_File = $sSrc_File, $iDescending = 0, $iStart = 1)
    Local $avArray, $iRet
    
    _FileReadToArray($sSrc_File, $avArray)
    If @error Then Return SetError(@error, 0, 0)
    
    ;Func __ArraySortNums(ByRef $avArray, $i_descending = 0, $iStart = 1)
    Local $iSE, $iHold
    
    For $iCount = $iStart To UBound($avArray) - 2
        $iSE = $iCount

        For $xCount = $iCount To UBound($avArray) - 1
            If ($iDescending And Number($avArray[$iSE]) < Number($avArray[$xCount])) Or _
            (Not $iDescending And Number($avArray[$iSE]) > Number($avArray[$xCount])) Then $iSE = $xCount
        Next

        $iHold = $avArray[$iCount]
        
        $avArray[$iCount] = $avArray[$iSE]
        $avArray[$iSE] = $iHold
    Next
    ;EndFunc ;__ArraySortNums
    
    $iRet = _FileWriteFromArray($sDest_File, $avArray, 1)
    SetError(@error, 0, $iRet)
EndFunc

And afaik, we don't have a simple _FileSortLines function as well...

Edited by MrCreatoR

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

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