in reply to Making use of a hash of an array...
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Making use of a hash of an array...
by Peter Keystrokes (Beadle) on Jul 18, 2017 at 21:50 UTC | |
by haukex (Archbishop) on Jul 19, 2017 at 12:17 UTC | |
by Peter Keystrokes (Beadle) on Jul 19, 2017 at 20:52 UTC | |
by haukex (Archbishop) on Jul 23, 2017 at 20:44 UTC | |
|
Re^2: Making use of a hash of an array...
by Peter Keystrokes (Beadle) on Jul 19, 2017 at 19:54 UTC | |
by 1nickt (Canon) on Jul 19, 2017 at 20:47 UTC | |
by Peter Keystrokes (Beadle) on Jul 19, 2017 at 20:57 UTC |