printf ("%08d",$accession);#which is before foreach loop. foreach (@accession){ .... $item1=....; $item2=....; printf("%16s%08s",$item1,$item2); }