Sign in to follow this  
Followers 0
DavidFromLafayette

Convert table to array utility

7 posts in this topic

I inherited some code that includes a large array (lots of rows and three columns) that needs to be modified. I am guessing I could export the existing array and get into an Excel sheet and make the necessary modifications by hand or using sed and awk but I am hoping that someone already created a script to read in a table and create an array from it with one or two clicks.

Everything I have found so far is reading in arrays from files while the program is being executed. In this case that is not really an option (I don't trust people to not modify [aka "fix"] the table contents).

If there is any such utility please point me in that direction.

thanks in advance

Share this post


Link to post
Share on other sites



See the _Excel* functions in the help file. Like _ExcelWriteArray() or _ExcelWriteSheetFromArray().

:)


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

Share this post


Link to post
Share on other sites

See the _Excel* functions in the help file. Like _ExcelWriteArray() or _ExcelWriteSheetFromArray().

:)

Sorry I guess I wasn't clear.... say I have a table in a text file as shown below, is there an easy way to get it into an array format as shown below that... Pretend the three lines I show goes on forever...

test1,707,5400

test2,949,5400

test3,1323,5400

Global $BCPMList[2150][3] = [["test1","707","5400"],["test2","949","5400"],["test3","1323","5400"]

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Here is some piece of code that should get you started

#include <Array.au3>

Local $Count = 0
Dim $BCPMList[1][3]

$file = FileOpen("FileToArray.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
    $line = StringSplit($line, ',')
    If IsArray($line) And $line[0] = 3 Then
        If $Count > UBound($BCPMList) - 1 Then ReDim $BCPMList[UBound($BCPMList) + 1][3]
        $BCPMList[$Count][0] = $line[1]
        $BCPMList[$Count][1] = $line[2]
        $BCPMList[$Count][2] = $line[3]
        $Count += 1
    EndIf
Wend
FileClose($file)

_ArrayDisplay($BCPMList, UBound($BCPMList))
Edited by Danny35d

AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line

Share this post


Link to post
Share on other sites

Sorry I guess I wasn't clear.... say I have a table in a text file as shown below, is there an easy way to get it into an array format as shown below that... Pretend the three lines I show goes on forever...

test1,707,5400

test2,949,5400

test3,1323,5400

Global $BCPMList[2150][3] = [["test1","707","5400"],["test2","949","5400"],["test3","1323","5400"]

#Include <Array.au3>

$Text = _
"test1,707,5400" & @CRLF &  _
"test2,949,5400" & @CRLF & _
"test3,1323,5400"
$2DArray = StringSplit2D($Text,@LF,",")
_ArrayDisplay($2DArray, "2DArray")

Func StringSplit2D($Text,$RowsDelimiters,$ColsDelimiters)
Local $2DArray[1][1] , $MaxCols = 0
$RowsSplit = StringSplit($Text,$RowsDelimiters)
For $i = 1 To $RowsSplit[0]
$ColSplit = StringSplit($RowsSplit[$i],$ColsDelimiters)
For $j = 1 To $ColSplit[0]
if $j > $MaxCols Then $MaxCols = $j
ReDim $2DArray[$i][$MaxCols]
$2DArray[$i - 1][$j - 1] = $ColSplit[$j]
Next
Next
Return $2DArray
EndFunc

صرح السماء كان هنا

 

Share this post


Link to post
Share on other sites

If it's a simple CSV format, Excel can open the file directly and import it:

#include <File.au3>
#include <Excel.au3>

$sText = "test1,707,5400" & @CRLF & "test2,949,5400" & @CRLF & "test3,1323,5400" & @CRLF
$sTempCSV = _TempFile(@ScriptDir, "~", ".csv")
FileWrite($sTempCSV, $sText)

$oExcel = _ExcelBookOpen($sTempCSV)

:)


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

Share this post


Link to post
Share on other sites

This is what I ended up using, thanks for the help and pointers.

#include <Array.au3>

#include<File.au3>

Local $Count = 0

Dim $BCPMList[1][3]

$file = FileOpen("BCPM_FLOW_RANGES.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

$line = StringSplit($line, ',')

If IsArray($line) And $line[0] = 3 Then

If $Count > UBound($BCPMList) - 1 Then ReDim $BCPMList[uBound($BCPMList) + 1][3]

$BCPMList[$Count][0] = $line[1]

$BCPMList[$Count][1] = $line[2]

$BCPMList[$Count][2] = $line[3]

$Count += 1

EndIf

WEnd

FileClose($file)

_ArrayDisplay($BCPMList, UBound($BCPMList))

ConsoleWrite(UBound($BCPMList))

$sFile = @ScriptDir & "\Array for Turbine listing.txt"

$arrayfile = FileOpen($sFile, 2)

FileWrite($sFile, "Global $BCPMList[" & UBound($BCPMList) & ",3] = [")

For $i = 0 To UBound($BCPMList) - 1

ConsoleWrite($i)

FileWrite($sFile, "[" & $BCPMList[$i][0] & "," & $BCPMList[$i][1] & "," & $BCPMList[$i][2] & "],")

Next

FileWrite($sFile, "]")

FileClose($sFile)

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
Sign in to follow this  
Followers 0