my @a=(); foreach (`cat file`){ $a[0]=$_; foreach $a[1](@b){ $a[1]$_; foreach $a[2](grep (/4/, @c)){ $a[2]=$_ }}}