Question

How do I import a multi-dimensional array from a tab-separated file?

  1. The file has no headers, It may have a blank or a dummy first-line.
  2. Should create a simple array and Not a HashTable.

TSV file:

-- empty / irrelevent first-line
"X" "0" "A"
"Y" "1" "B"

such that: array[0][0] = "X" & array[1][2] = "B"

Was it helpful?

Solution

$m=gc c:\temp\matrix.txt
$array=@()

$m | Foreach{
    $elements=$_.split("`t")
    $array+= ,@( $elements[0],$elements[1],$elements[2])
}
$array[0][0]
$array[1][2]

note the comma (line 6) that force powershell to consider the added line as an array

To ignore the first line you can do something like this

$matrix=""
(1..($m.count-1)) | foreach{ $matrix+=$m[$_]}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top