Jump to content

Recommended Posts

Posted

i have a script with an array. it has to read the array values from a text file instead of  from within the script. the text file has 58k lines of words from which i need to have as a result only 1 word at a time. so now it has to read from the text file only 1 line at a time and write it as a result to another text file

 

#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
#include <File.au3>
#include <Date.au3>


Global $result1s[3]=["text1", "text2", "text3"]

_Main()

Func _Main()

    Local $button1
    Local $output, $die, $msg, $results1
    Local $file = FileOpen("test.txt", 1)
    Local $g_idEdit

    GUICreate("test", 600, 200, -1, -1)
    $button1 = GUICtrlCreateButton("Result", 460, 110, 50, 30)

    $output1 = GUICtrlCreateInput("", 60, 30, 450, 20, BitOR($ES_CENTER, $ES_READONLY))

    $g_idEdit = GUICtrlCreateEdit("", 60, 10, 450, 20, $SS_LEFT)

    $die = GUICtrlCreateLabel("", 700, 500, 700, 20, $SS_SUNKEN)
    GUICtrlSetFont($output, 8, 800, "", "Verdana")
    GUISetState()



    ; Run the GUI until the dialog is closed
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $button1

                $results1 = Random(1, 2, 1)
                GUICtrlSetData($output1, $result1s[$results1])
                $read1 = GUICtrlRead($output1)


            FileWriteLine($file, _NowDate()& " " & _nowTime() & " " &GUICtrlRead($g_idEdit))
            FileWriteLine($file, _NowDate()& " " & _nowTime() & " " &$read1)



        EndSelect
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    WEnd
EndFunc   ;==>_Main

 

  • Moderators
Posted

way1000,

If you want to read a file one line at a time, take a look at FileReadLine.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Posted

Also, could you maybe give an example of, like, three lines of input, and the required three lines of output? There may be much better ways to code this kind of thing.

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Posted

I am intrigued.  58k lines, which you want to parse one word at a time.  Pray tell why?  If this is ten words per line, you are looking at ~580 000 words.  

Skysnake

Why is the snake in the sky?

Posted

Indeed. It smells like the kind of thing one might be much better off doing through powershell or maybe some Cygwin-based linux magic :) Although of course AutoIt is fine as well.

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Posted (edited)

way1000,

A couple of examples of reading a file to an array using regex.

#include <array.au3>

local $sFile1 = @scriptdir & '\file1.txt'
local $sFile2 = @scriptdir & '\file2.txt'

; one word per line

local $aResult = stringregexp(fileread($sFile1),'.*\R',3)
_arraydisplay($aResult,'One Word per line')


; multiple words per line, varying delimiters (@CRLF, @TAB, space and comma)

local $aResult = stringregexp(fileread($sFile2),'(?m)(\b[^\s,]+\b)',3)
_arraydisplay($aResult,'Multiple Words per line')

In the future it will serve you well to provide examples of your input, your code (or a reproducer) and what you expect as a result.

Good Luck,

kylomas

edit - files used for testing...file1.txt file2.txt

In the interest of completeness a non sre version...

#include <array.au3>
#include <StringConstants.au3>

local $sFile1 = @scriptdir & '\file1.txt'

local $aResult = stringsplit(fileread($sFile1),@CRLF,$STR_ENTIRESPLIT)
_arraydisplay($aResult,'One Word per line')

 

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Posted
27 minutes ago, AutoBert said:

and here a sre version for file2:

Where?! Wheeeeeerrrre?! :hyper:

Roses are FF0000, violets are 0000FF... All my base are belong to you.

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