my @final = grep {@$_} map { my $key = $_; [ grep{ m/^\d\|\d\|\d\|$key\|\d$/ } @records ] } @keys;