Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333] : superdoc . print w/replies, xml ) Need Help??

Let's assume you have split out the 4 elements as variables $id1, $id2, $sequence, $label. The next thing you need to create is the signature that represents a "unique" value, by combining $id2 and $sequence: simplest is if you can join them with some character known not to appear in either value - from the example above I will guess that the pipe character '|' is safe to use:

my $signature = join '|', $id2, $sequence;

Now you can use this signature as the key in a hash. For simplicity, I'll use this to store the entire structure:

my %hash; # somewhere before you start to loop over the data ... # within the loop over your data my $signature = join '|', $id2, $sequence; my $structure = { id1 => $id1, id2 => $id2, sequence => $sequence, label => $label, }; $hash{$signature} = $structure; # save it

In the case of duplicate signatures this overwrites, so ends up saving a structure for the last example of any given signature, but there are other strategies possible.

You can then emit the data by looping over the hash something like:

for my $signature (keys %hash) { my $structure = $hash{$signature}; printf "%s|%s\n%s\n%s\n", $structure->{id1}, $structure->{id2}, $structure->{sequence}, $structure->{label}; }

In reply to Re: How to make unique entries by hv
in thread How to make unique entries by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.