$dat0 = 'a.txt'; open(DAT, "$dat0") || die("Could not open file!"); @all=; @temp = ''; $temp[0] = $temp[1] = ''; $l = 1; foreach $i (@all) { $temp[$l * 2] = $i; @u = split (/ /, $i); foreach $j (@u){ if (($j ne "$j") || ($j ne "v")) { @tep = '';$tep1 = 0; foreach $k (@all) { @uuu = split (/ /, $k); chomp $j; if ($uuu[0] eq "$j") { $tep[$tep1] = \$k; $tep1++;} } $temp[$l * 2 + 1] = \@tep; } } $l++;} open(DAT, ">$dat0") || die; print DAT @all; close(DAT);