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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |