I have a list like this:

name1: group1
name2: group4
name3: group1 group2
name4: group4
..........
..........


I wish to invert this list, and make it look like:

group1: name1 name3...
group2: name3
group4: name2, name4
...........
...........


Can somebody offer at least a psuedo code?
Thanks

I have a list like this:

name1: group1
name2: group4
name3: group1 group2
name4: group4
..........
..........


I wish to invert this list, and make it look like:

group1: name1 name3...
group2: name3
group4: name2, name4
...........
...........


Can somebody offer at least a psuedo code?
Thanks

Cool problem. Can you post the code that shows how your list is structured? If it's a hash then I would create an alternate hash and figure out how to assign the names to it.

You basically want to emulate a SELECT DISTINCT clause in Sql to get your groups. Maybe create an array that holds all of the distinct names, add a name to the list if and only if it doesn't exist. So your code may look something like:

# this will be part perl part pseudo code:
@groups;
$distinct_flag; # a flag to see if we have a distinct value in an array
while (($key, $value) = each(%list)){
# where key is name and group is value
# so here we want to run through our groups array and see if our value is in it:
foreach $group (@groups)
if ($value ne $group)
{
   $distinct_flag = "true";
}
else 
{ distinct_flag = "false"; }
} # end foreach
if {distinct_flag = "true" }
push (@groups, $value}
} #endwhile

so now that you have those lists. Use a variation on the above code to go through your list, obitain groups where your value equals your name, and assign it thusly. Will help more later if needed

I took a swing at this in C# since I don't do much Perl.
I put the data into a file and read it in 1 line at a time.
I created a hash table (Dictionary) containing a string as the "key" and a list of strings as the "Value".

As the program loops through the file, it splits the data into parts.
Each group becomes a KEY in the new hash and the name becomes the VALUE.

If the key does not exist, it adds a new key.
If it does exist, it adds a new data row to the key.

That way, ANY groups or names that are added to this are automatically captured.

To display the contents, it can do a double-loop through the keys and data.

Let me know if you want to see the C# code.

May I suggest the following? I am assuming:

1) That any given name occurs only once in the original list;
2) That neither "names" nor "groups" themselves contain spaces;

#!/usr/bin/perl -w

use strict;


my %INV_LIST;
while (<>) {
        my($old_key, @old_val) = split(/:*\s+/);
       
        foreach my $new_key(@old_val) {
                $INV_LIST{$new_key} .= " ".$old_key;
        }
}       

foreach my $key (sort(keys(%INV_LIST))) {
        print $key.":".$INV_LIST{$key}."\n";
}

The data split into two parts. The second part split by space and the data stored in to hash format.

$hash{'each second data'} .= "first part data ";

### Data splited and store in hash format
map($hash{$_}.="$1 ", (split / /, $2)) while (<DATA>=~ m{(.*?): ([^\n]+)}gs);

### print the hashes data 
print "$_: $hash{$_}\n" for sort keys %hash;

__DATA__
name1: group1
name2: group4
name3: group1 group2
name4: group4
commented: nice sol. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.