use strict; use warnings; my @lines = ( "??????????????", "123" ); my $conv; for my $line ( @lines ){ $conv=""; my @c = split('', $line); $conv .= ($c[$_] || 0) for 0..5; print "$conv\n"; }