Create new variable from existing one. replace delimiter with break and text

Hi all

Beginner question. I have a variable as follows:

$MyVariable = @{}
$MyVariable[‘MyValue’] = “one, two, three”

Name Value


MyValue one, two, three

I’d like to create a new variable and have the value “one, two, three” splited with a line break and added some text. The end result “MyValue2” should look as follows:

1.2.3.4 = “{text}”
fixtext = “one&”
fixtext = “two&”
fixtext = “three&”

I’m a newbe in PS and would appreciate some hints how to achieve this. Only “one”, “two” and “three” is pulled from the first variable the rest is additional fix text. Thank you for pointing me in the right direction.

Cheers, Chris

 

Not really sure what you are trying to accomplish, but you are working with a hash table.

$MyVariable['MyValue'] = "one, two, three"

Keep in mind that this is a string:

“one, two, three”

and this is an array:

“one”, “two”, “three”

These are parsed differently, so you need to understand what you are working with. With what you posted, a string, you would need to use -split to make the string into an array to loop through it and since you have a space after the comma we need to trim it to get “one” versus "one ".

$MyVariable = @{}
$MyVariable['MyValue'] = "one, two, three"

foreach ($item in $MyVariable.GetEnumerator()) {
    'Processing item {0}' -f $item.Name

    foreach ($val in ($item.Value -split ',')) {
        'Processing value {0}&' -f $val.Trim()
    }

}

Hi Rob

Thanks you so much for your code. It helped me a bit but still one thing I’m not sure how to manage. It seems you have two loops but since this is only part of the variables this will mess things up. Here the adapted code which is working for me:

$MyVariable = @{}
$MyVariable.MyValue = "one, two, three"
$MyVariable.OtherValue = "whatever"
$MyVariable.Etc = "and so on"



$MyVariable.MyValue2 = foreach ($val in ($MyVariable.MyValue -split ',')) {
'My fix text "{0}&' -f $val.Trim() +"`""
}

$MyVariable
Name Value
---- -----
MyValue2 {My fix text "one&", My fix text "two&", My fix text "three&"}
OtherValue whatever
MyValue one, two, three
Etc and so on



$MyVariable.MyValue2
My fix text "one&"
My fix text "two&"
My fix text "three&"


That is what I tried to achieve, but there is one “fix text line” at the top of $MyVariable.MyValue2.

 

 

If I understood you right I think you’ve mixed up some different misconceptions. :wink: … try the following snippet.

$MyVariable = @{ }
$MyVariable.MyValue = "one, two, three"
$MyVariable.OtherValue = "whatever"
$MyVariable.Etc = "and so on"

$MyVariable.MyValue2 = foreach ($val in ($MyVariable.MyValue -split ',')) {
    "My fix text {0}" -f $val.Trim() 
}

$MyVariable.MyValue2

… and I think you can even simplify or “improve” it a little like so:

$MyVariable = @{ }
$MyVariable.MyValue = 'one', 'two', 'three'
$MyVariable.OtherValue = 'whatever'
$MyVariable.Etc = 'and so on'

$MyVariable.MyValue2 = foreach ($val in $MyVariable.MyValue) {
    'My fix text {0}' -f $val
}

$MyVariable.MyValue2