Jump to content

Filling Arrays from files


Recommended Posts

This matter what sort of array you want to use. In this example I suppose that is two-dimentional array, where first dim - rows, second - columns. It will ignore empty lines and not have any error checks :)

#include <File.au3>
$in_filename = "test.csv"

Dim $lines

_FileReadToArray($in_filename, $lines)

$temp = StringSplit($lines[1], ";"); Found columns number

Dim $array[ $lines[0] ][ $temp[0] ]

For $i = 1 To $lines[0]
    $temp = StringSplit($lines[$i], ";")
    If $temp[0] = 1 Then Continueloop
    For $j = 1 To $temp[0]
        $array[$i-1][$j-1] = $temp[$j]
    Next
Next
Link to comment
Share on other sites

This script does work, if you replace the ";" with "," both times;

;----------------------------------------------------------------------

;LazyCat script; modified Randall (StringSplit typo needs comma)

;----------------------------------------------------------------------

#include <File.au3>

;$in_filename = "test.csv"

$in_filename = FileOpenDialog("hi",@ScriptDir&"\SciTe","Files (*.csv)",5)

Dim $lines,$Display, $NumCols

_FileReadToArray($in_filename, $lines)

$temp = StringSplit($lines[1], ","); Found columns number

$NumCols=$temp[0]

Dim $array[ $lines[0] ][ $temp[0] ]

For $i = 1 To $lines[0]

    $temp = StringSplit($lines[$i], ",")

    If $temp[0] = 1 Then Continueloop

    For $j = 1 To $temp[0]

        $array[$i-1][$j-1] = $temp[$j]

    Next

Next

For $i = 1 To $lines[0]-1

;----------------------------------------------------------------------

;Display Array

;----------------------------------------------------------------------

    For $j = 1 To $NumCols

  $Display  = $Display&"array["&String($i-1)&"]["&String($j-1)&"]="&chr(9)&$array[$i-1][$j-1]&@CRLF

    Next

Next

MsgBox(4096, "Here it is",$Display)

Randall
Link to comment
Share on other sites

What happens if one of the cells values contains a , or a " ?

My opinion is that reading from a '.csv file' is not the best approach when communicating with Excel.

I recommend Excel VBA or COM.

Edited by bshoenhair
Link to comment
Share on other sites

My opinion is that reading from a '.csv file' is not the best approach when communicating with Excel.

I recommend Excel VBA or COM.

<{POST_SNAPBACK}>

using COM or vba is best; can you show me your scrip to get the array?

Thanks, Randall

(I see the ";", just was not sure if the poster was still having trouble understanding you could still use arrays....)

Link to comment
Share on other sites

using COM or vba is best; can you show me your scrip to get the array?

Thanks, Randall

<{POST_SNAPBACK}>

I like to think of a spreadsheet as a super multidimensional array so really there is no need to create an array since it is already there. I will give an example on how to read from the values column by column with COM but you will need AutoIts latest beta and Excel to run it.

$Excel = ObjCreate("Excel.Application"); Create an Excel Object
if @error then 
  Msgbox (0,"","Error creating Excel object. Error code: " & @error)
  exit
endif
if not IsObj($Excel) then 
  Msgbox (0,"ExcelTest","I'm sorry, but creation of an Excel object failed.")
  exit
endif
$Excel.Visible = 1   ; Let the guy show himself - Set to 0 to run excel invisible
$NewBook = $Excel.workbooks.add   ; Add a new workbook
; Example: Fast Fill some cells with there address
Msgbox (0,"","Click 'ok' to fastfill some cells")
For $c In $NewBook.Sheets("Sheet1").Range("a1:n1").Cells
    $c.Value = $c.Address
    For $i = 1 To 8
        $c.Offset($i,0).Value = $c.Offset($i,0).Address
    Next
Next

msgbox(0,"ExcelTest","Click 'ok' to read the cells in each column.")
$Values = ""
For $c In $NewBook.Sheets("Sheet1").Range("a1:n9").Cells
    $Values = $Values & $c.Text & @TAB
Next
msgbox(0,"Values Read",StringStripWS($Values,3))
$Excel.activeworkbook.saved = 1; To prevent 'yes/no' questions from Excel
$Excel.quit        ; Get rid of him.
$Excel = 0         ; Loose this object.
               ; Object will also be automatically discarded when you exit the script
exit
Edited by bshoenhair
Link to comment
Share on other sites

This script does work, if you replace the ";" with "," both times;

Randall

<{POST_SNAPBACK}>

I tried this script on a log file I have I get an error message

line 17

$array[$i-1][$j-1] = $temp[$j]

Error: Array variable has incorrect number of subscripts or subscript dimension range exceeded.

Whats wrong here

Link to comment
Share on other sites

I tried this script on a log file I have I get an error message

line 17

$array[$i-1][$j-1] = $temp[$j]

Error: Array variable has incorrect number of subscripts or subscript dimension range exceeded.

Whats wrong here

I had same error message on 1 machine only; this used to work; I am still not sure what happened

Randall

Edited by randallc
Link to comment
Share on other sites

You will have to change two lines. Change these two lines that start with $temp=StringSplit($lines... where ";" to " ". Also, you need to save the Excel file as a tab-delimited file (*.txt) to make it work.

$temp = StringSplit($lines[1], ";"); Found columns number

Dim $array[ $lines[0] ][ $temp[0] ]

For $i = 1 To $lines[0]
    $temp = StringSplit($lines[$i], ";")

Last, I had trouble initialy trying to use tabs. The trouble was empty cells in the Excel file would cause data to move to the left.

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