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