I'm not sure I understand your specification as to what the conditions are for "captures the hairpin best", but as I understand it, it has something to do with how the ranges overlap? In that case, here are some threads that might be of interest: Calculate the overlap length between two ranges, Range overlap (with a lot of ranges), or finding intermediate range values from two file columns (in the latter I mention Interval trees, which might be useful here). If your input data is large, that might mean you need a different data structure for efficient lookups.

As for reading the file into a data structure, that's probably easiest with a simple state machine type approach, in this example I'm keeping the state (the current key) as $curkey:

use warnings; use strict; use Data::Dump; my %hash; my $curkey; while (<DATA>) { chomp; if (/^>(\w+)$/) { $curkey = $1; } elsif ( my ($data,$low,$high) = /^\s*(\w+)\s+(\d+)\s*\.\.\s*(\d+)\s*$/ ) { die "value seen before header: '$_'" unless defined $curkey; push @{ $hash{$curkey} }, { data=>$data, low=>$low, high=>$high }; } else { die "don't know how to parse: '$_'" } } dd \%hash; __DATA__ >key1 Foo 10 .. 20 Bar 11 .. 21 >key2 Quz 30 .. 40 Baz 37 .. 45

Update: Minor tweaks to regex and text.


In reply to Re: Making use of a hash of an array... by haukex
in thread Making use of a hash of an array... by Peter Keystrokes

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.