while (my $str = ) { chomp($str); $comp = $1 if $str =~ m{(.*?)}; $cmd = $1 if $str =~ m{(.*?)}; if ($str =~ m{(.*?)}) { push(@{$hoh{$comp}{$cmd}}, $1); } } __OUTPUT__ $VAR1 = { 'ip' => { 'ip1' => [ '/tftpboot/tc1' ], 'ip2' => [ '/tftpboot/tc1', '/tftpboot/tc3', '/tftpboot/tc4' ] }, 'parser' => { 'p2' => [ '/tftpboot/tc1' ], 'p3' => [ '/tftpboot/tc1', '/tftpboot/tc2', '/tftpboot/tc3' ], 'p1' => [ '/tftpboot/tc1', '/tftpboot/tc2' ] } };