my ($ita,$fren,$num1,$num2)= split(/[=\s,]+/); #creates col of numbers $hash{$ita}[1]=$fren; #now hash_ref format will look like this: ita=> spa , fren #define if there is are numbers in position 3 and 4 concat to position 3 if (defined ($num1)){ $hash{$ita}[3]=$num1; # % looks like this: Ita=> Spa, Fren, RandNUM[3] } if (defined $num2){ $hash{$ita}[4]=$num2; # % looks like this Ita=> Span,Fren, Randnum[3]?(ifdefined), Randnum[4] } }