%deps = ( 1 => { 2 => undef, 3 => undef }, 2 => { 2 => undef, 4 => undef }, 3 => { 1 => undef, 4 => undef }, 5 => { 2 => undef, 3 => undef }, ); #### my %deps; while(){ my @cols = split; my $task = shift @cols; $deps{$task}->{$_} = undef for @cols; # or another way: %{$deps{$task}} = map { $_ => undef } @cols; }