Hello guys,
I’m new in Powershell and I’m stuck to import values to a matrix:
I’ve a variable that receive values in this format:
$content
[ “value1”, “value2”, “value3”,“value4”,“value5”]
how can I move to a matrix:
$list= @'{
A =" “;
B =” “;
C =” “;
D =” “;
E =” ";
}
to have this result
$list.A > value 1
$list.B > value 2
$list.C > value 3
$list.D > value 4
$list.E > value 5
Best Regards
Alessandro
Please edit your post using the </>
button to format your code properly. This makes it much easier for people to read and to copy/paste for testing.
I’m a bit confused by your question as the sample data doesn’t really explain what you’re after. Please can you clarify:
Is $content
a string object like this '["value1","value2","value3","value4","value5"]'
or an array of values like this @('value1','value2','value3','value4','value5')
?
You say you want a matrix, but a matrix is a multi-dimensional array and you’re using the syntax for a dictionary or hashtable, which is made up of key/value pairs. The hashtable would seem more appropriate but what is it that you actually want?
When outputting the results, do you want the greater than >
symbol in the output?
Hello Matt,
thank you so much for your reply.
$url.content is a value received from a Invoke-WebRequest. I receive it with a format
[“value1”,“value2”,“value3”,“value4”,“value5”]’
I need to use the “value5” as filename.
how can I export the values?
Treat it as a string and split it on the comma:
This will remove the [
]
and "
:
$splits = $url.content -replace '\[|\]|"' -split ','
You can then process the $splits
array which will contain
'value1','value2','value3','value4','value5'
So if you wanted to get it into a hashtable you could do the following. Because I’m not sure if you always have five values, I use a variable so that I can start at ‘A’ and go up through the alphabet as needed:
$ht = [ordered]@{}
$i = 0
$charVal = 65
while ($i -lt $splits.Count) {
$ht.Add(
([CHAR]($charVal+$i)).ToString(),$splits[$i]
)
$i++
}
$ht
Name Value
---- -----
A value1
B value2
C value3
D value4
E value5
To access an individual value in the table, specifiy the key:
$ht['A']
value1
2 Likes
thank you so much Matt!!! It’s work.