Link to home
Start Free TrialLog in
Avatar of D B
D BFlag for United States of America

asked on

Load Data From Parameter File into Array of Strings

I am reading data from a parameter file such that I am reading a key and value pair. When finished, one group of values I have in PowerShell is something like:
$STRING1 = GetParameterValue $PARMS "KEY1"  -- $PARMS is an array of key/value pairs
$STRING2 = GetParameterValue $PARMS "KEY2"
...

What I want to end up with is an array (e.g. $Strings) so that I can do something like:

foreach($s in $Strings)
{
    processes...
}

What is the best way to get a group of individual string values into the array?
Avatar of footech
footech
Flag of United States of America image

An array of key/value pairs - that is a hashtable.
Say you have a file called stringdata.txt with the following:
Msg1 = Value1
Msg2 = Value2
Msg3 = Value3

Open in new window


You can read that into a hashtable with the following:
$PARMS = Get-Content stringdata.txt | Out-String | ConvertFrom-StringData

Open in new window

You can then reference a value associated with a key like
$PARMS["Msg1"]

Open in new window

Avatar of D B

ASKER

Current code segment is:
function GetParameterValue($ParmFile, $ParmName)
{
	$tmp = $ParmFile | where { $_.Key -eq $ParmName } | Select Value
	[string]$result = $tmp.value
	return $result.trim()
}

$PARMS = import-csv .\CreateDatabase.properties

$Value = GetParameterValue $PARMS "Key" 
...

Open in new window


I am reading about 15 values but only 10 of them I want to be put into a string array. So assume that key/value pairs 1..5 are standalone values used elsewhere and 6..15 are to be read into the string array so they can be parsed using the foreach syntax. The other five values are used for other purposes. I have the code already in place to read all the values, now I just want to get the 10 into an array of string values. I don't want to rewrite the parsing code because it is pretty much standard and used in a number of places.
Avatar of D B

ASKER

The file contains the key value pairs is in the format of:
key1,value1
key2,value2
...
but that should be irrelevant because, like I said, I already have all the parsing code for it in place, the intent is to get value6..value15 into an array of strings.
I'm having trouble understanding your use case and what variations may occur.
Here's a shot though.
You can collect all the values into an array like this.
$array = "key1","key2","key7" | ForEach { GetParameterValue $PARMS $_ }

Open in new window

You can use Select-Object to get the first or last X values, or skip X values.
$array | Select -Last 10

Open in new window

Avatar of D B

ASKER

I have 10 distinct variables with values:
$string1
$string2
$string3
...
$string10

I want to get the values of those 10 variables into an array. The code I provided above is pretty much a template of how we read a config file of properties, so I do not want to vary from that (how the file is processed and the values are parsed. What I want to do is get the values of 10 variables into a single array of strings so I can use foreach()

Look at it as though how these 10 variables came to be or how their values were obtained is irrelevant. The use case is to get the values of 10 variables into an array of strings. And to make it simple so that 10 can become 5 or 20 somewhat easily. I used $string1..$string10 as examples, but they might as well be $myvara, $somethingelse, $bubbles, $grapes, $ten, $whoareyou, $idontknow, $down, $left, $gone. Those are 10 variables whose values I want to put into a single string array, the same way I would build $array = @("one", "two", "three"...)
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of D B

ASKER

I will likely use $array = $Value1,$Value2,$Value3, but I like the idea of: $array = "key1","key2","key3" | ForEach { GetParameterValue $PARMS $_ }.
Although the process of reading the parameter file is set, I can use the normal method to read the values not associated with the array and use the second method to read into the array. Either would be easy to maintain. I am sure the resources involved in either method would be similar.
Thanks.