Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I have an AutoIt Script that i need to get the Columns in CSV file and load them into an array (Possibly a multidimensional array or array of arrays). I have used the CSV.au3 functions by ProgAndy which can load the rows into an array just fine, but I need the rows in an array.

Thanks in advance for any help.

EDIT

To elaborate, Currently my csv file looks like the following.

Item 1, Item 2, Another Item
Item 3, Item 4

When I parse the CSV using CSV.au3 by ProgAndy it creates a multidimensional array that looks like the following.

$aResult[0] = [0] => 'Item 1', [1] => 'Item 2', [2] => 'Another Item'
$aResult[1] = [0] => 'Item 3', [1] => 'Item 4'

Where I truly would like the arrays to look like the following.

$aResult[0] = [0] => 'Item 1', [1] => 'Item 3'
$aResult[1] = [0] => 'Item 2', [1] => 'Item 4'
$aResult[2] = [0] => 'Another Item'

Meaning that for each array it will contain the column not the row.

share|improve this question
2  
The question makes no sense. You say you need them in an array but you already have them in an array? So what's the problem? – Robin Green Oct 27 '13 at 10:24
    
I think he meant that he successfully read the rows using the CSV.au3 but according to the title of the question he's trying to read columns into an array. – mrt Oct 27 '13 at 21:24
    
@mrt is correct, i have edited the question to explain a bit better. – Michael Smith Oct 29 '13 at 1:10

3 Answers 3

Here is CSV to 2D Array example. Credits to Malkey.

#include <array.au3>

Opt("MustDeclareVars", 1)

Dim $sCSV = '"010","2","03"' & @CRLF & _
        '"24","30","20"' & @CRLF & _
        '"txt","bla","toto"'

Local $arr = _CsvToArray2D($sCSV)
_ArrayDisplay($arr)

; Converts CSV format to a 2D array.
Func _CsvToArray2D($sCSV)
    Local $aTmp = StringRegExp($sCSV & @CR, '(\V*)\v{1,2}', 3)
    Local $NumCols[UBound($aTmp)]
    For $x = 0 To UBound($aTmp) - 1
        StringReplace($aTmp[$x], ",", ",")
        $NumCols[$x] = @extended + 1
    Next
    Local $Max = _ArrayMax($NumCols, 1)

    Dim $aArr[UBound($aTmp)][$Max]

    For $i = 0 To UBound($aArr, 1) - 1
        Local $aTemp = StringSplit($aTmp[$i], ",")
        For $j = 0 To $aTemp[0] - 1
            $aArr[$i][$j] = $aTemp[$j + 1]
        Next
    Next
    Return $aArr
EndFunc  ;==>_CsvToArray2D
;========> End of _CsvToArray2D ======================
share|improve this answer

no need for includes

params:

  1. filename
  2. delimeter
  3. message display if cannot open file
  4. logical true/false to skip the first line of the file

;_ParseCSV("filename",",","message if error happens",true)

Func _ParseCSV($f,$Dchar,$error,$skip)

  Local $array[500][500]
  Local $line = ""

  $i = 0
  $file = FileOpen($f,0)
  If $file = -1 Then
    MsgBox(0, "Error", $error)
    Return False
   EndIf

  ;skip 1st line
  If $skip Then $line = FileReadLine($file)

  While 1
       $i = $i + 1
       Local $line = FileReadLine($file)
       If @error = -1 Then ExitLoop
       $row_array = StringSplit($line,$Dchar)
        If $i == 1 Then $row_size = UBound($row_array) 
        If $row_size <> UBound($row_array) Then  MsgBox(0, "Error", "Row: " & $i & " has different size ")
        $row_size = UBound($row_array)
        $array = _arrayAdd_2d($array,$i,$row_array,$row_size)

   WEnd
  FileClose($file)
  $array[0][0] = $i-1 ;stores number of lines
   $array[0][1] = $row_size -1  ; stores number of data in a row (data corresponding to index 0 is the number of data in a row actually that's way the -1)
   Return $array

EndFunc
Func _arrayAdd_2d($array,$inwhich,$row_array,$row_size)
    For $i=1 To $row_size -1 Step 1
        $array[$inwhich][$i] = $row_array[$i]
  Next
  Return $array
   EndFunc
share|improve this answer
up vote 0 down vote accepted

Ok, i figured this one out a few months ago, once I had a break from this project for a while, and ended up figuring it out myself.

Below is the solution I came up with:

;Load the first line into an array (for our Count Later)
$columnsCounter = StringSplit($content[1], ",")
;Here we use the row count (from $content) and column count (from $columnsCounter)
;We define an array that is the perfect size for our Menu Items
Dim $MenuItems[$content[0] + 1][$columnsCounter[0] + 1]

; Now we loop through each row and column
For $x = 1 To ($content[0]) - 1
    ;Create an array with the row we are looking at
    $oneRow = StringSplit($content[$x], ",")
    ;now loop through each column
    For $y = 1 To ($columnsCounter[0])
        ;Grab the item we want and add it to our menu items array
        $MenuItems[$x][$y] = $oneRow[$y]
    Next
Next
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.