Jump to content

Import text file and store into an array


 Share

Recommended Posts

Hey everyone. If anyone could help me out, greatly appreciated. How can you import a text file and take each word and have it stored into an array. The array then would be read one element at a time and have each word entered into another program. I know how to take the words and put it into another program, but can't figure out how to import a text file and have the words put into an array.

Thanks

Link to comment
Share on other sites

  • Moderators

Hey everyone. If anyone could help me out, greatly appreciated. How can you import a text file and take each word and have it stored into an array. The array then would be read one element at a time and have each word entered into another program. I know how to take the words and put it into another program, but can't figure out how to import a text file and have the words put into an array.

Thanks

You can get the beta version and use _FileReadToArray() or just use the function itself below:
;===============================================================================
;
; Description:    Reads the specified file into an array.
; Syntax:          _FileReadToArray( $sFilePath, $aArray )
Local = $sArray = ''; Parameter(s):  $sFilePath - Path and filename of the file to be read
;                  $aArray  - The array to store the contents of the file
; Requirement(s):   None
; Return Value(s):  On Success - Returns 1
;                  On Failure - Returns 0 and sets @error = 1
; Author(s):        Jonathan Bennett <jon at hiddensoft com>
; Note(s):        None
;
;===============================================================================
Func _FileReadToArray($sFilePath, ByRef $aArray)
;==============================================
; Local Constant/Variable Declaration Section
;==============================================
    Local $hFile
    
    $hFile = FileOpen($sFilePath, 0)
    
    If $hFile = -1 Then
        SetError(1)
        Return 0
    EndIf
    
    $aArray = StringSplit( StringStripCR( FileRead($hFile, FileGetSize($sFilePath))), @LF)
    
    FileClose($hFile)
    Return 1
EndFunc;==>_FileReadToArray

Edit: Then assuming words are seperated by spaces:

Local $filePath = 'location of file'
Local = $aArray = ''
_FileReadToArray($filePath, $aArray)
Local = $sArray = _SeperateEachWord($aArray); should have each 'word' now in an array

Func _SeperateEachWord(ByRef $aArray)
    For $i = 1 To Ubound($aArray) - 1
        $sArray = $sArray & StringSplit($aArray[$i], ' ') & Chr(01)
    Next
    Return StringSplit(StringTrimRight($sArray, 1), Chr(01))
EndFunc
Not Tested! 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

If each word is on a separate line, separated by @CRLF (the endline char), you can use _FileReadToArray and get each word in the array that way.

If the words are not separated, you could do it this way:

- read the whole file into a string.

- remove all endline characters (if there are any)

- stringsplit the string with the splitting character being a space

The result of the StringSplit is an array containing every word.

Link to comment
Share on other sites

This is how my data appears in the .txt file

ACI AHT AL BAC BBT

BDN BJS BLS CCU CD

CNP COP CRE CSC CSE

CSR ECA EDP EWZ EXR

FD HCA HDI HMT IT

JNJ KEA KNL KO L

LMT LU MGM MXO NT

OHI OXY PFE PKD PQE

RIG SWK TSM TXU UVN

VLO VTR WFR WRI YSI

Edited by chandler8
Link to comment
Share on other sites

  • Moderators

ACI AHT AL BAC BBT

BDN BJS BLS CCU CD

CNP COP CRE CSC CSE

This is how my data appears in the .txt file

CSR ECA EDP EWZ EXR

FD HCA HDI HMT IT

JNJ KEA KNL KO L

LMT LU MGM MXO NT

OHI OXY PFE PKD PQE

RIG SWK TSM TXU UVN

VLO VTR WFR WRI YSI

Then what I wrote and edited might work ... again I hadn't tested it.

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

Local $filePath = 'location of file'
Local $aArray = ''
_FileReadToArray($filePath, $aArray)
Local $sArray = _SeperateEachWord($aArray); should have each 'word' now in an array

Func _SeperateEachWord(ByRef $aArray)
    For $i = 1 To Ubound($aArray) - 1
        $sArray = $sArray & StringSplit($aArray[$i], ' ') & Chr(01)
    Next
    Return StringSplit(StringTrimRight($sArray, 1), Chr(01))
EndFunc

Quick edit of Smoke's code - there shouldn't have been Local = $sArray.

Link to comment
Share on other sites

  • Moderators

Local $filePath = 'location of file'
Local $aArray = ''
_FileReadToArray($filePath, $aArray)
Local $sArray = _SeperateEachWord($aArray); should have each 'word' now in an array

Func _SeperateEachWord(ByRef $aArray)
    For $i = 1 To Ubound($aArray) - 1
        $sArray = $sArray & StringSplit($aArray[$i], ' ') & Chr(01)
    Next
    Return StringSplit(StringTrimRight($sArray, 1), Chr(01))
EndFunc

Quick edit of Smoke's code - there shouldn't have been Local = $sArray.

:o, that's what I get for typing in the forum window!!, thanks green!

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

lol

Thanks for your help. I have loaded the data into an array and I can then send this to text file or whatever I need to do. However, how can I load the text file and have each individual word from that text file stored into an individual element of an array. This is my Data:

ACI AHT AL BAC BBT

BDN BJS BLS CCU CD

CNP COP CRE CSC CSE

CSR ECA EDP EWZ EXR

This is the Code:

#include <Array.au3>

;Description: Reads the specified file into an array.

; Syntax: _FileReadToArray( $sFilePath, $aArray )

Local $sArray = ''; Parameter(s): $sFilePath - Path and filename of the file to be read

; $aArray - The array to store the contents of the file

; Requirement(s): None

; Return Value(s): On Success - Returns 1

; On Failure - Returns 0 and sets @error = 1

; Author(s): Jonathan Bennett <jon at hiddensoft com>

; Note(s): None

;

;===============================================================================

Func _FileReadToArray($sFilePath, ByRef $aArray)

;==============================================

; Local Constant/Variable Declaration Section

;==============================================

Local $hFile

$hFile = FileOpen($sFilePath, 0)

If $hFile = -1 Then

SetError(1)

Return 0

EndIf

$aArray = StringSplit( StringStripCR( FileRead($hFile, FileGetSize($sFilePath))), @LF)

Run("notepad.exe")

WinWaitActive("Untitled - Notepad")

For $j = 1 To Ubound($aArray) - 1

Sleep(1000)

Send($aArray[$j])

Send("{ENTER}")

Next

WinClose("Untitled - Notepad")

FileClose($hFile)

Return 1

EndFunc;==>_FileReadToArray

Local $filePath = 'D:\Stocks.txt'

Local $aArray = ''

_FileReadToArray($filePath, $aArray)

Local $sArray = _SeperateEachWord($aArray); should have each 'word' now in an array

Func _SeperateEachWord(ByRef $aArray)

For $i = 1 To Ubound($aArray) - 1

$sArray = $sArray & StringSplit($aArray[$i], ' ') & Chr(01)

Next

Return StringSplit(StringTrimRight($sArray, 1), Chr(01))

EndFunc

The result from reading the text file is:

ACI AHT AL BAC BBT element 1

BDN BJS BLS CCU CD element 2

CNP COP CRE CSC CSE element 3

CSR ECA EDP EWZ EXR element 4

This is how I would like the data: Having trouble figuring out how to get each word into its own element of and array.

ACI element 1

AHT element 2

AL element 3

BAC element 4

BBT element 5

BDN element 6

etc.

Link to comment
Share on other sites

  • Moderators

Sorry, I shouldn't have done mine in the forum window, here, this is tested:

Local $filePath = @DesktopDir & '\WordArray.txt'
Local $aArray = ''
_FileReadToArray($filePath, $aArray)
Local $sArray = _SeperateEachWord($aArray)
; should have each 'word' now in an array, second paramater you put what seperates each word you want in the array
; in this case it is a space ' ', but I made it optional, the standard is set for a space ' '

For $x = 1 To UBound($sArray) - 1
    MsgBox(0, 'Test', $sArray[$x])
Next

Func _SeperateEachWord(ByRef $aArray, $dDelimeter = ' ')
    For $i = 1 To Ubound($aArray) - 1
        If $aArray[$i] <> '' Then 
            $SplitSpace = StringSplit($aArray[$i], $dDelimeter)
            For $x = 1 To UBound($SplitSpace) - 1
                $sArray = $sArray & $SplitSpace[$x] & Chr(01)
            Next
        EndIf
    Next
    Return StringSplit(StringTrimRight($sArray, 1), Chr(01))
EndFunc

Func _FileReadToArray($sFilePath, ByRef $aArray)
    Local $hFile
    $hFile = FileOpen($sFilePath, 0)
    If $hFile = -1 Then
        SetError(1)
        Return 0
    EndIf
    $aArray = StringSplit( StringStripCR( FileRead($hFile, FileGetSize($sFilePath))), @LF)
    FileClose($hFile)
    Return 1
EndFunc;==>_FileReadToArray

Edit:

Added an optional parameter to _SeperateEachWord(), it is set to a space ' ' as the standard, but you can change it to whatever seperates the words when calling it if it is something other than a space.

Edit2:

And if you want to see the array displayed (I don't know if you need beta for ArrayDisplay()

#include <array.au3>
Local $filePath = @DesktopDir & '\WordArray.txt'
Local $aArray = ''
_FileReadToArray($filePath, $aArray)
Local $sArray = _SeperateEachWord($aArray)
; should have each 'word' now in an array, second paramater you put what seperates each word you want in the array
; in this case it is a space ' ', but I made it optional, the standard is set for a space ' '

#cs
For $x = 1 To UBound($sArray) - 1
    MsgBox(0, 'Test', $sArray[$x])
Next
#ce

_ArrayDisplay($sArray, 'Words Array')

Func _SeperateEachWord(ByRef $aArray, $dDelimeter = ' ')
    For $i = 1 To Ubound($aArray) - 1
        If $aArray[$i] <> '' Then 
            $SplitSpace = StringSplit($aArray[$i], $dDelimeter)
            For $x = 1 To UBound($SplitSpace) - 1
                $sArray = $sArray & $SplitSpace[$x] & Chr(01)
            Next
        EndIf
    Next
    Return StringSplit(StringTrimRight($sArray, 1), Chr(01))
EndFunc

Func _FileReadToArray($sFilePath, ByRef $aArray)
    Local $hFile
    $hFile = FileOpen($sFilePath, 0)
    If $hFile = -1 Then
        SetError(1)
        Return 0
    EndIf
    $aArray = StringSplit( StringStripCR( FileRead($hFile, FileGetSize($sFilePath))), @LF)
    FileClose($hFile)
    Return 1
EndFunc;==>_FileReadToArray
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

Hey thanks. You Rock!!! Works great!!! One thing with the data being spit out. It comes out with a lot of space in between the next 5 symbols

Is there anyway to space them evenly.

Here's what the data looks like:

ACI

AHT

AL

BAC

BBT

BDN

BJS

BLS

CCU

CD

Link to comment
Share on other sites

  • Moderators

I have no idea what your asking, do you have an example?

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

Ok!!! Thanks

Here's the code:

#include <Array.au3>

Func _FileReadToArray($sFilePath, ByRef $aArray)

Local $hFile

$hFile = FileOpen($sFilePath, 0)

If $hFile = -1 Then

SetError(1)

Return 0

EndIf

$aArray = StringSplit( StringStripCR( FileRead($hFile, FileGetSize($sFilePath))), @LF)

FileClose($hFile)

Return 1

EndFunc;==>_FileReadToArray

Local $filePath = 'D:\Stocks.txt'

Local $aArray = ''

_FileReadToArray($filePath, $aArray)

Local $sArray = _SeperateEachWord($aArray); should have each 'word' now in an array

Run("notepad.exe")

WinWaitActive("Untitled - Notepad")

For $x = 1 To UBound($sArray) - 1

Sleep(1000)

Send($sArray[$x])

sleep(1000)

Send("{Enter}")

;MsgBox(0, 'Test', $sArray[$x])

Next

WinClose("Untitled - Notepad")

Func _SeperateEachWord(ByRef $aArray, $dDelimeter = ' ')

For $i = 1 To Ubound($aArray) - 1

If $aArray[$i] <> '' Then

$SplitSpace = StringSplit($aArray[$i], $dDelimeter)

For $x = 1 To UBound($SplitSpace) - 1

$sArray = $sArray & $SplitSpace[$x] & Chr(01)

Next

EndIf

Next

Return StringSplit(StringTrimRight($sArray, 1), Chr(01))

EndFunc

Here's the Data: call the text file Stocks and run the program.

ACI AHT AL BAC BBT

BDN BJS BLS CCU CD

CNP COP CRE CSC CSE

CSR ECA EDP EWZ EXR

FD HCA HDI HMT IT

JNJ KEA KNL KO L

LMT LU MGM MXO NT

OHI OXY PFE PKD PQE

RIG SWK TSM TXU UVN

VLO VTR WFR WRI YSI

The output looks like this: I know I can fix the output by manipulating the data in the original text file to having only one column.

ACI

AHT

AL

BAC

BBT

BDN

BJS

BLS

CCU

CD

CNP

COP

CRE

CSC

CSE

Link to comment
Share on other sites

  • Moderators

Well that looks like what I gave you.. but what I wrote doesn't seperate anything, I used your example

ACI AHT AL BAC BBT

BDN BJS BLS CCU CD

CNP COP CRE CSC CSE

CSR ECA EDP EWZ EXR

FD HCA HDI HMT IT

JNJ KEA KNL KO L

LMT LU MGM MXO NT

OHI OXY PFE PKD PQE

RIG SWK TSM TXU UVN

VLO VTR WFR WRI YSI

when I made it. Also, can you use code tags [ code] [ /code] without the space after '[' when displaying code.

Using this:

Local $filePath = @DesktopDir & '\WordArray.txt'
Local $aArray = ''
_FileReadToArray($filePath, $aArray)
Local $sArray = _SeperateEachWord($aArray)
; should have each 'word' now in an array, second paramater you put what seperates each word you want in the array
; in this case it is a space ' ', but I made it optional, the standard is set for a space ' '

ConsoleWrite('Word Array' & @LF & '[0] = ' & UBound($sArray) - 1 & @LF)
For $x = 1 To UBound($sArray) - 1
    ConsoleWrite('[' & $x & '] = ' & $sArray[$x] & @LF)
Next


Func _SeperateEachWord(ByRef $aArray, $dDelimeter = ' ')
    For $i = 1 To Ubound($aArray) - 1
        If $aArray[$i] <> '' Then
            $SplitSpace = StringSplit($aArray[$i], $dDelimeter)
            For $x = 1 To UBound($SplitSpace) - 1
                $sArray = $sArray & $SplitSpace[$x] & Chr(01)
            Next
        EndIf
    Next
    Return StringSplit(StringTrimRight($sArray, 1), Chr(01))
EndFunc

Func _FileReadToArray($sFilePath, ByRef $aArray)
    Local $hFile
    $hFile = FileOpen($sFilePath, 0)
    If $hFile = -1 Then
        SetError(1)
        Return 0
    EndIf
    $aArray = StringSplit( StringStripCR( FileRead($hFile, FileGetSize($sFilePath))), @LF)
    FileClose($hFile)
    Return 1
EndFunc;==>_FileReadToArray

I got this output from the console within SciTe

Word Array

[0] = 50

[1] = ACI

[2] = AHT

[3] = AL

[4] = BAC

[5] = BBT

[6] = BDN

[7] = BJS

[8] = BLS

[9] = CCU

[10] = CD

[11] = CNP

[12] = COP

[13] = CRE

[14] = CSC

[15] = CSE

[16] = CSR

[17] = ECA

[18] = EDP

[19] = EWZ

[20] = EXR

[21] = FD

[22] = HCA

[23] = HDI

[24] = HMT

[25] = IT

[26] = JNJ

[27] = KEA

[28] = KNL

[29] = KO

[30] = L

[31] = LMT

[32] = LU

[33] = MGM

[34] = MXO

[35] = NT

[36] = OHI

[37] = OXY

[38] = PFE

[39] = PKD

[40] = PQE

[41] = RIG

[42] = SWK

[43] = TSM

[44] = TXU

[45] = UVN

[46] = VLO

[47] = VTR

[48] = WFR

[49] = WRI

[50] = YSI

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

Hey just wanted to thank you again for your great advice. The problem was with the data that I was copying and then pasting into my text file. After correcting the problem, my results are identical in the SciTE.

Thanks again.......

Question for ya. Can you keep focus on a say a text file or another program that you want your script to run on while opening other windows....

Link to comment
Share on other sites

  • Moderators

Hey just wanted to thank you again for your great advice. The problem was with the data that I was copying and then pasting into my text file. After correcting the problem, my results are identical in the SciTE.

Thanks again.......

Question for ya. Can you keep focus on a say a text file or another program that you want your script to run on while opening other windows....

You can put the function calls in a loop, or use another script to do it. AdlibEnable wouldn't work here (the way it's documented, and I don't have time to explain at the moment how to do it with it), because you need to pass parameters.

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