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

Welcome Forums General PowerShell Q&A Create new variable from existing one. replace delimiter with break and text

Viewing 3 reply threads
  • Author
    Posts
    • #201729
      Participant
      Topics: 1
      Replies: 1
      Points: 13
      Rank: Member

      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

       

    • #201777
      Participant
      Topics: 12
      Replies: 1489
      Points: 1,987
      Helping Hand
      Rank: Community Hero

      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()
          }
      
      }
      
    • #201819
      Participant
      Topics: 1
      Replies: 1
      Points: 13
      Rank: Member

      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.

       

       

    • #201828
      Participant
      Topics: 4
      Replies: 1908
      Points: 4,259
      Helping Hand
      Rank: Community Hero

      If I understood you right I think you’ve mixed up some different misconceptions. 😉 … 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
Viewing 3 reply threads
  • You must be logged in to reply to this topic.