943,929 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 1181
  • Perl RSS
Oct 30th, 2009
0

[PROBLEM] inverting a list

Expand 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jacquelinek is offline Offline
3 posts
since Oct 2009
Oct 30th, 2009
0
Re: [PROBLEM] inverting a list
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:
Perl Syntax (Toggle Plain Text)
  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
Reputation Points: 31
Solved Threads: 4
Junior Poster in Training
eggmatters is offline Offline
67 posts
since Nov 2008
Oct 30th, 2009
0
Re: [PROBLEM] inverting a list
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.
Featured Poster
Reputation Points: 290
Solved Threads: 277
Posting Virtuoso
thines01 is online now Online
1,684 posts
since Oct 2009
Oct 30th, 2009
0
Re: [PROBLEM] inverting a list
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;
Perl Syntax (Toggle Plain Text)
  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; Oct 30th, 2009 at 6:45 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
baslow is offline Offline
2 posts
since Oct 2009
Nov 3rd, 2009
1
Re: [PROBLEM] inverting a list
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 ";

Perl Syntax (Toggle Plain Text)
  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
Reputation Points: 55
Solved Threads: 24
Junior Poster in Training
k_manimuthu is offline Offline
93 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: Perl regex
Next Thread in Perl Forum Timeline: Visual Basic.Net Data types





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC