Created
August 5, 2013 20:19
-
-
Save standage/6159218 to your computer and use it in GitHub Desktop.
Given a mapping (in the form of a tab-delimited text file), collapse the mapping so that each key occupies a single line.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env perl | |
| # Copyright (c) 2013, Daniel S. Standage <daniel.standage@gmail.com> | |
| # | |
| # input: two columns of data in tab-delimited format, mapping from a key | |
| # (column 1) to a value (column 2); | |
| # output: also two columns of data in tab delimited format, but all values | |
| # sharing the same key are printed on a single line; that is, each key | |
| # corresponds to a comma-separated list of associated values | |
| use strict; | |
| my %data; | |
| while(<STDIN>) | |
| { | |
| chomp; | |
| my @values = split(/\t/); | |
| $data{ $values[0] }->{ $values[1] } = 1; | |
| } | |
| foreach my $key(sort keys %data) | |
| { | |
| my $valuelist = $data{$key}; | |
| my @values = sort keys %$valuelist; | |
| my $valuestring = join(",", @values); | |
| printf("%s\t%s\n", $key, $valuestring); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment