loading an array with a csv file

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
jsira2003@yahoo.com
Posts: 117
Last visit: Tue Jul 11, 2023 6:18 am

loading an array with a csv file

Post by jsira2003@yahoo.com »

Typically I load a csv file into an array. Turns out I need to create the same array table but this time I want to build the array table from within powershell and not from an external file. I thought of using hash tables. I don't believe this is the best method. Then I considered building a data table. Once again I am not sure this is the best method. Do you have any suggestions how I may be able to accomplish my goal? Is there any easy way to convert a hash table to an array?

thank you,
John
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: loading an array with a csv file

Post by jvierra »

Easiest way is to create custom objects for each row.

[pscustomobject]@{
Col1 = 'some value'
Col2 = 'other value'
… etc ...
}
User avatar
jsira2003@yahoo.com
Posts: 117
Last visit: Tue Jul 11, 2023 6:18 am

Re: loading an array with a csv file

Post by jsira2003@yahoo.com »

When you say $array = import-csv..., what type of table is created? How does that compare to the datatable that we created. Is it an array, system.collections.arraylist, hashtable, a data table or something else?

I ask because when I tried to send the table to a routine create as a data table it failed. I suspect it might have failed due to mistyping. I haven't had time to review it yet.

thanks,
John
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: loading an array with a csv file

Post by jvierra »

A CSV can be imported and converted to a DataTable.

$array = Import-Csv <file>
$dt = ConvertTo-DataTable $array


What problems are you having with this?
User avatar
jsira2003@yahoo.com
Posts: 117
Last visit: Tue Jul 11, 2023 6:18 am

Re: loading an array with a csv file

Post by jsira2003@yahoo.com »

I fixed it.

After creating the table I had to convert it to an array:

[array]$motors = $table

where table is a database. It appears the = was able to convert the table to an array without any fuss. Without this step my application failed.

John
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: loading an array with a csv file

Post by jvierra »

If you have a single object and not an array it cannot be converted to a DataTable.

The easy protection against singletons is the following:

[array]$csv = Import-Csv <file>
$dt = ConvertTo-DataTable $array]/b]

You will also see this at time:

$csv = @(Import-Csv <file>)

So yes. You have to explicitly force a singleton to an array.

Object collections in PowerShell are not tables. They are just collections. When a command returns a singleton it will not be returned as an array except for some CmdLets that always return collections.

For a discussion of this see: https://social.technet.microsoft.com/Forums/en-US/0bbbd689-a8d3-420a-a8f3-9a03e21fd8c3/array-and-hashtables-output?forum=winserverpowershell
This topic is 5 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked