markdavis87 has asked for the wisdom of the Perl Monks concerning the following question:
Alright, so I have a question for all you Perl gurus out there... I have a program that yields a set of data, in a certain format. The program is written in Perl, and I would like to append some code to the program that will sort the data it produces a certain way. Here is what the original output looks like:
ALPHA:D 20 letters ABCCEDFFGAACDDDEEEFG ALPHA:D 20 letters ABCCEDFFGAACDDDEEEFG ALPHA:D 20 letters ABCCEDFGGAACDDDEEEFG ALPHA:D 20 letters ABCCEDFGGAACDDDEEEFG ALPHA:D 20 letters ABCCEDFGGAACDDDEEEFG ALPHA:D 20 letters ABCCEDFGGAACDDDEEEFG ALPHA:D 20 letters ABCCEDFFGAACDDEEEEFG ALPHA:D 20 letters ABCCEDFFGAACDDDEEEFG ALPHA:D 20 letters ABCCEDFFGAACDDEEEEFG ALPHA:E 24 letters ABCCEDFFGAACDDDEEEFGAGAD ALPHA:E 24 letters ABCCEDFFGAACDDDEEEFGAGAD ALPHA:E 24 letters ABCCEDFFGAACDDDEEEFGAGAD ALPHA:E 24 letters ABCCEDFFGAACDDDEEEFGAGAD ALPHA:E 24 letters ABCCEDFFGAACDDDEEEFGAGAD ALPHA:E 24 letters ABCCEDFFGAACDDDEEEFGAGAE ALPHA:E 24 letters ABCCEDFFGAACDDDEEEFGAGAE ALPHA:E 24 letters ABCCEDFFGAACDDDEEEFGAGAE ALPHA:E 24 letters ABCCEDFFGAACDDDEEEFGAGAE ALPHA:E 24 letters ABCCEDFFGAACDDDEEEFGAGAE ALPHA:E 24 letters ABCCEDFFGAACDDDEEEFGAGAE ALPHA:E 24 letters ABCCEDFFGAACDDDEEEFGAGAE
Each field is tab-delimited (except for the space between "20" and "letters", for example, which is just a space). I'm looking for a few lines of code that will sort the data so that it looks like this:
ALPHA:D 20 letters ABCCEDFGGAACDDDEEEFG 4 ALPHA:D 20 letters ABCCEDFFGAACDDDEEEFG 3 ALPHA:D 20 letters ABCCEDFFGAACDDEEEEFG 2 ALPHA:E 24 letters ABCCEDFFGAACDDDEEEFGAGAE 7 ALPHA:E 24 letters ABCCEDFFGAACDDDEEEFGAGAD 5
As you can see, what it's doing is looking at each value (e.g., "D" or "E") listed next to the initial name ("ALPHA", in this case), and counting the number of unique arrangements of letters for that specific value. Please note that while a line may contain the same number of letters (e.g., "20 letters"), it may not have the same arrangement. Certain letters might be different in the string. For value "D", the arrangement "ABCCEDFFGAACDDDEEEFG" appears 3 times, but not necessarily in order. The arrangement "ABCCEDFGGAACDDDEEEFG" appears 4 times. The code should get the counts, order them with the highest counts first, then move to the next value. I am assuming that there are some really basic string manipulation commands that can do this quite easily, but I am by no means an expert in Perl, so I have no idea how this would work. Could any of you help me out here? I would greatly appreciate it!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: String sorting in Perl
by Laurent_R (Canon) on Jun 04, 2014 at 17:34 UTC | |
by Limbic~Region (Chancellor) on Jun 04, 2014 at 17:38 UTC | |
by Laurent_R (Canon) on Jun 04, 2014 at 17:48 UTC | |
by Limbic~Region (Chancellor) on Jun 04, 2014 at 18:06 UTC | |
by markdavis87 (Novice) on Jun 04, 2014 at 18:14 UTC | |
by markdavis87 (Novice) on Jun 04, 2014 at 18:23 UTC | |
by Laurent_R (Canon) on Jun 04, 2014 at 18:26 UTC | |
by markdavis87 (Novice) on Jun 04, 2014 at 18:29 UTC | |
by Laurent_R (Canon) on Jun 04, 2014 at 18:47 UTC | |
| |
by markdavis87 (Novice) on Jun 04, 2014 at 19:06 UTC | |
by Anonymous Monk on Jun 04, 2014 at 18:02 UTC | |
Re: String sorting in Perl
by perlfan (Parson) on Jun 05, 2014 at 13:09 UTC |