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
loading an array with a csv file
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.
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.
- jsira2003@yahoo.com
- Posts: 117
- Last visit: Tue Jul 11, 2023 6:18 am
- jsira2003@yahoo.com
- Posts: 117
- Last visit: Tue Jul 11, 2023 6:18 am
Re: loading an array with a csv file
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
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
- jsira2003@yahoo.com
- Posts: 117
- Last visit: Tue Jul 11, 2023 6:18 am
Re: loading an array with a csv file
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
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
Re: loading an array with a csv file
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
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