[PROBLEM] inverting a list

Reply

Join Date: Oct 2009
Posts: 3
Reputation: jacquelinek is an unknown quantity at this point 
Solved Threads: 0
jacquelinek jacquelinek is offline Offline
Newbie Poster

[PROBLEM] inverting a list

 
0
  #1
29 Days Ago
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 63
Reputation: eggmatters is an unknown quantity at this point 
Solved Threads: 4
eggmatters eggmatters is offline Offline
Junior Poster in Training
 
0
  #2
29 Days Ago
Originally Posted by jacquelinek View Post
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:
  1. # this will be part perl part pseudo code:
  2. @groups;
  3. $distinct_flag; # a flag to see if we have a distinct value in an array
  4. while (($key, $value) = each(%list)){
  5. # where key is name and group is value
  6. # so here we want to run through our groups array and see if our value is in it:
  7. foreach $group (@groups)
  8. if ($value ne $group)
  9. {
  10. $distinct_flag = "true";
  11. }
  12. else
  13. { distinct_flag = "false"; }
  14. } # end foreach
  15. if {distinct_flag = "true" }
  16. push (@groups, $value}
  17. } #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
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 91
Reputation: thines01 is an unknown quantity at this point 
Solved Threads: 8
thines01 thines01 is offline Offline
Junior Poster in Training
 
0
  #3
29 Days Ago
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: baslow is an unknown quantity at this point 
Solved Threads: 0
baslow baslow is offline Offline
Newbie Poster
 
0
  #4
29 Days Ago
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;
  1. #!/usr/bin/perl -w
  2.  
  3. use strict;
  4.  
  5.  
  6. my %INV_LIST;
  7. while (<>) {
  8. my($old_key, @old_val) = split(/:*\s+/);
  9.  
  10. foreach my $new_key(@old_val) {
  11. $INV_LIST{$new_key} .= " ".$old_key;
  12. }
  13. }
  14.  
  15. foreach my $key (sort(keys(%INV_LIST))) {
  16. print $key.":".$INV_LIST{$key}."\n";
  17. }
Last edited by baslow; 29 Days Ago at 6:45 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 10
Reputation: k_manimuthu is an unknown quantity at this point 
Solved Threads: 2
k_manimuthu k_manimuthu is offline Offline
Newbie Poster
 
0
  #5
24 Days Ago
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 ";

  1. ### Data splited and store in hash format
  2. map($hash{$_}.="$1 ", (split / /, $2)) while (<DATA>=~ m{(.*?): ([^\n]+)}gs);
  3.  
  4. ### print the hashes data
  5. print "$_: $hash{$_}\n" for sort keys %hash;
  6.  
  7. __DATA__
  8. name1: group1
  9. name2: group4
  10. name3: group1 group2
  11. name4: group4
Reply With Quote Quick reply to this message  
Reply

Message:



Other Threads in the Perl Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC