Ittiam Systems Interview Question
Software TraineesTeam: dsfgfds
Country: India
Interview Type: Written Test
#convert no to word
use Data::Dumper;
# Solution one without using regex
$no =1568999;
%map =( 1 => "one",
2 => "two",
3 => "three",
4 => "four",
5 => "five",
6 => "six",
7 => "seven",
8 => "eight",
9 => "nine",
0 => "zero",
);
@digits = split(//,$no);
foreach(@digits){
$result .= $map{$_}." ";
}
print("The result without using regex is : $result");
# Solution two using regex
while($no =~ /([0-9])/g)
{
$result1 .= $map{$1}." ";
}
print("The result using regex is :".$result1);
It's easier than that:
- Henry January 17, 2013# use original posters map function.
# use original posters $no definition.
my $newstr;
($newstr=$no) =~ s/(\d)/$map{$1}/g;
print "$newstr\n";
Of course there are variations such as inserting spaces, etc, but the above doesn't require loops but could be a simple function that is called from a loop that, say, parses @ARGV or a file.