use strict; use warnings; my %DB; my $store = ""; my $head; while () { if (/.*/ && ! /Last modified/){ #If line is a title my $temp = $_; $_=~ s/(<[^<>]+>)+//g; chomp; if (/\(/){ $store = $_; $DB{$store} = $temp; }else{ $store = $_; $DB{$store} = $temp; } }else{ #Not a title ($store ne "")? $DB{$store}:$head .= $_; } } print join "\n", map {"$_ -> $DB{$_}"} keys %DB; __DATA__ Title line a line another line this line that line