How to add values to an array ...

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of Sam Boutros Sam Boutros 2 months, 1 week ago.

  • Author
    Posts
  • #68554
    Profile photo of Jean-Marc
    Jean-Marc
    Participant

    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
    Profile photo of Olaf Soyk
    Olaf Soyk
    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
      Profile photo of Jean-Marc
      Jean-Marc
      Participant

      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
    Profile photo of Sam Boutros
    Sam Boutros
    Participant
    #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
      Profile photo of Jean-Marc
      Jean-Marc
      Participant

      Works like a charm !

      thanks you so much !

  • #68587
    Profile photo of Sam Boutros
    Sam Boutros
    Participant

    For more information on arrays see this article..

You must be logged in to reply to this topic.