"Can you please advise what kind of structure I need to use?"
You can use simple hash (%uniq in code below); take advantage of the fact that hash keys are unique; and form those keys by joining string1 & string2 with a character that won't appear in either string (I used a tab; change that to something else if your strings can contain tabs).
#!/usr/bin/env perl use strict; use warnings; use constant { REC_SEP => "\n//\n", KEY_SEP => "\t", OUT_FMT => "%s\n%s\n%s\n//\n", }; my %uniq; { local $/ = REC_SEP; while (<DATA>) { chomp; my ($id, $s1, $s2) = split; $uniq{join KEY_SEP, $s1, $s2} = $id; } } printf OUT_FMT, $uniq{$_}, split(KEY_SEP) for keys %uniq; __DATA__ nick AAAAAAAAAA BBBBBBBBBB // george EGRGERHTETEHTHR VFRTTHTRRHTE // andreas AAAAAAAAAA BBBBBBBBBB // thomas EWTRYTUYJTHT CEWWEQRWT$G // peter EGRGERHTETEHTHR VFRTTHTRRHTE //
Output from sample run:
andreas AAAAAAAAAA BBBBBBBBBB // peter EGRGERHTETEHTHR VFRTTHTRRHTE // thomas EWTRYTUYJTHT CEWWEQRWT$G //
Note that hashes are unordered. Although the output shown may appear to be ordered, on a couple of other runs that I tested I got thomas/andreas/peter & thomas/peter/andreas (and with sufficient test runs, I would have got all possible orders). If the output order is important to you, just add some sorting; e.g.
... for sort { $uniq{$a} cmp $uniq{$b} } keys %uniq;
— Ken
In reply to Re: Make unique hash of arrays?
by kcott
in thread Make unique hash of arrays?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |