How to add values to an array ...

Welcome Forums General PowerShell Q&A How to add values to an array ...

This topic contains 5 replies, has 3 voices, and was last updated by

 
Participant
1 year, 7 months ago.

  • Author
    Posts
  • #68554

    Participant
    Points: 0
    Rank: Member

    Hello world!

    I'm completely newbie in Powershell and i need your help ...
    I created 3 variables containing values separated by ";" :

    $process = processname
    $Sresult = Sdata1;Sdata2;Sdata3;Sdata4;Sdata5 ....
    $Iresult = Idata1;Idata2;Idata3;Idata4;Idata5 ....

    I would like to have an array or hash table like this (i don't know what is the best solution) :

    Process Sresult Iresult
    processname Sdata1 Idata1
    processname Sdata2 Idata2

    and so on ... at the end of course a small CSV output is planned. 🙂 ..
    Thanks !

  • #68563

    Participant
    Points: 144
    Helping Hand
    Rank: Participant

    It might be helpful to know how you created this variables. It might be easier to create what you're looking for directly in the process of getting the information than stitching it together afterwards. (sorry for my poor english)

    • #68581

      Participant
      Points: 0
      Rank: Member

      Hi Olaf

      I retrieve these values from a complex xml-like file with several tags with a regex ... these tags don't have any ID ...
      e.g. :

      suppliers
      COM-09 To perform Operative Planning

      inputs

      Validated Operative Plan (full view & Cascaded)

      I use this "really-dirty" foreach loop to parse all the lines :

      # Retrieve the Suppliers
      "**************** Suppliers **************************"
      $pattern = "(?:)(.*)(?:)"
      foreach ($suppliers in $XmlDocument.metadata.entry.map.entry.map.entry)
         {
          if ($suppliers.string -eq "suppliers")
            {
            $result = [Regex]::Match($suppliers.string, $pattern)
            $Sresult = $Sresult += $result.Value.replace('','').replace('','')
            $Sresult = $Sresult -replace "&","&"
            $Sresult = $Sresult + ";"
            }
          if ($suppliers.string -eq "inputs")
            {
            $result = [Regex]::Match($suppliers.string, $pattern)
            $Iresult = $Iresult += $result.Value.replace('','').replace('','')
            $Iresult = $Iresult -replace "&","&"
            $Iresult = $Iresult + ";"
          }
        }
  • #68583

    Participant
    Points: 59
    Rank: Member
    #region Input
    $Process = 'one','two','three'
    $Color   = 'green','blue','yellow'
    $Memory  = 2,4,8
    #endregion
    
    #region Process
    # Put them in a Powershell Object:
    $myOutput = 0..2 | foreach {
        [PSCustomObject]@{
            Process = $Process[$_]
            Color   = $Color[$_]
            Memory  = $Memory[$_]
        }
    }
    #endregion
    
    #region Output
    $myOutput | Format-Table -AutoSize                      # output to console
    $myOutput | Out-GridView                                # output to ISE gridview
    $myOutput | Export-Csv .\mystuff.csv -NoTypeInformation # dump to CSV
    #endregion
    
    • #68586

      Participant
      Points: 0
      Rank: Member

      Works like a charm !

      thanks you so much !

  • #68587

    Participant
    Points: 59
    Rank: Member

    For more information on arrays see this article..

The topic ‘How to add values to an array ...’ is closed to new replies.