Solved

Load Data From Parameter File into Array of Strings

Posted on 2015-01-23
7
115 Views
Last Modified: 2015-01-23
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?
0
Comment
Question by:dbbishop
  • 4
  • 3
7 Comments
 
LVL 39

Expert Comment

by:footech
ID: 40567608
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

0
 
LVL 15

Author Comment

by:dbbishop
ID: 40567615
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.
0
 
LVL 15

Author Comment

by:dbbishop
ID: 40567617
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.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 39

Expert Comment

by:footech
ID: 40567648
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

0
 
LVL 15

Author Comment

by:dbbishop
ID: 40567662
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"...)
0
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
ID: 40567704
It does matter how the variables came to be or else you're left with no option but manually adding each variable to the array explicitly.
Whether you do
$Value1 = GetParameterValue $PARMS "Key1"
$Value2 = GetParameterValue $PARMS "Key2"
$Value3 = GetParameterValue $PARMS "Key3"
$array = $Value1,$Value2,$Value3

or
$array = @()
$array += $Value1 = GetParameterValue $PARMS "Key1"
$array += $cheese = GetParameterValue $PARMS "Key2"
$array += $burger = GetParameterValue $PARMS "Key3"

or
$array = "key1","key2","key3" | ForEach { GetParameterValue $PARMS $_ }
doesn't matter.  The last way is just a condensed version.

If you've already got $Value1, $Value2, and $Value3 (or whatever they're named) and don't want to modify your code that you've shown above, then the only way to put them in an array is like below, explicitly adding each by name.
$array = $Value1,$Value2,$Value3

For this to be expandable to many variables, you either have to have a record of all the variable names, or have some similarity in how they are named so that you could run a command like
$array = Get-Variable String* | Select -expand Value

If possible, it's better to collect all the values into an array before you assign individual pieces to variables.
0
 
LVL 15

Author Closing Comment

by:dbbishop
ID: 40567744
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.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now