| | |
[PROBLEM] inverting a list
![]() |
•
•
Join Date: Nov 2008
Posts: 63
Reputation:
Solved Threads: 4
0
#2 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
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)
# 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
•
•
Join Date: Oct 2009
Posts: 91
Reputation:
Solved Threads: 8
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.
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.
•
•
Join Date: Oct 2009
Posts: 2
Reputation:
Solved Threads: 0
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) 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)
#!/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"; }
Last edited by baslow; 29 Days Ago at 6:45 pm.
•
•
Join Date: Jun 2009
Posts: 10
Reputation:
Solved Threads: 2
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 ";
$hash{'each second data'} .= "first part data ";
Perl Syntax (Toggle Plain Text)
### 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
![]() |
Other Threads in the Perl Forum
- Previous Thread: Perl regex
- Next Thread: Visual Basic.Net Data types
| Thread Tools | Search this Thread |





