$ perl -MData::Dumper -e"%h=(a=>{1..4},b=>{5..8});print Dumper\%h" $VAR1 = { 'a' => { '1' => 2, '3' => 4 }, 'b' => { '7' => 8, '5' => 6 } }; #### use strict; use warnings; use Data::Dumper; ... my %hash; while () { chomp; my @line = split /\s+/, $_; my ($key1,$key2) = (shift @line, shift @line); $hash{$key1} = $hash{$key2} = [@line]; } print Dumper \%hash;