Multivalued Array – concatenate double values

Welcome Forums General PowerShell Q&A Multivalued Array – concatenate double values

  • This topic has 5 replies, 3 voices, and was last updated 1 month ago by
    Participant
    .
Viewing 4 reply threads
  • Author
    Posts
    • #278847
      Participant
      Topics: 1
      Replies: 2
      Points: 16
      Rank: Member

      Hello, been bugging me all day and I cannot seem to find a decent solution … I hope the powershell community can help me.

      I have the following array:

      col1 Col2
      ab data1
      cd data2
      ef data1
      ab data2
      cd data3

      The array is created from a csv which is imported through ‘Import-CSV’ with -Delimiter “,” and -HeaderĀ  “Col1″,”Col2”.
      The goal is to rework this Array to concatenate the value from ‘Col2’ for the double entries in ‘Col1’, preferably into a new array.
      The output would then look like

      Col1 Col2
      ab data1 | data2
      cd data2 | data3
      ef data1

      I’ve been fuzzing around with a lot of code snippets but nothing that gives me the desired output.

       

    • #278889
      Participant
      Topics: 4
      Replies: 422
      Points: 734
      Helping Hand
      Rank: Major Contributor

      If it’s only two columns, you could import the CSV into a hashtable and see if the key (column 1) already exists, if it does, add it to existing key (hashtables can’t contain duplicate keys).

       

    • #278892
      Participant
      Topics: 5
      Replies: 349
      Points: 632
      Helping Hand
      Rank: Major Contributor
    • #279774
      Participant
      Topics: 1
      Replies: 2
      Points: 16
      Rank: Member

      Matt, thank you for your reply. I was also looking at that solution, but that didn’t do the trick.

      I must say I forgot to mention there are more then 2 columns.

      Finally, the code from random commandline did the trick.

      A user, identified by his login, can be multiple times in the received csv file. Each time with a different group.
      Goal was to create a new Array with unique logins (no doubles) with all the groups for the user grouped in one Array element for that login
      Finally, it looks like this:

      The Out-String comes with a “new line”, henceĀ  “-Replace(‘[\n]’, “|”)
      TrimEnd(“|”)” to remove the final ‘|’

      Thank you!

      • This reply was modified 1 month ago by jhn01.
    • #279795
      Participant
      Topics: 1
      Replies: 2
      Points: 16
      Rank: Member

      thank you for your input.
      Matt, i was already trying this piece of code, but it didn’t do the trick. I forgot to mention in my original post that there are more then 2 columns.
      Finally, the code from Random Commandline did the trick.

      Input is a csv with several columns, one containing group info.
      The csv can contain multiple lines for the same user, each representing a different group.
      Goal was to create a final array, only with all the unique users with a ‘group’ field per user containing all the groups (if there are multiple).

      The final code looks like this:

      -Replace(‘[\n]’, “|”) –> the outstring comes with a new line for each value
      .TrimEnd(“|”) –> to remove the final “|”

      Thank you all for your help.

Viewing 4 reply threads
  • You must be logged in to reply to this topic.