#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11144249 use warnings; my $file = 'data.11144249'; open my $fh, '<', $file or die "$! opening $file"; local $_ = do{ local $/; <$fh> }; # slurp entire file my %solvent_face; # as requested $solvent_face{ "$3_$1" } = $2 while /\b (F\S+) \h+ ([-\d.]+) (?=.*? (\S+) \h+ 0.00000000 )/gsx; # by solvent, then arrays of each F*** push @{ $solvent_face{ $3 }{ $1 } }, $2 while /\b (F\S+) \h+ ([-\d.]+) (?=.*? (\S+) \h+ 0.00000000 )/gsx; # by F***, then arrays of solvent push @{ $solvent_face{ $1 }{ $3 } }, $2 while /\b (F\S+) \h+ ([-\d.]+) (?=.*? (\S+) \h+ 0.00000000 )/gsx; use Data::Dump 'dd'; dd \%solvent_face;