regex - How do I retrieve an integer's ordinal suffix in Perl (like st, nd, rd, th) -
i have number , need add suffix: 'st', 'nd', 'rd', 'th'. example: if number 42 suffix 'nd' , 521 'st' , 113 'th' , on. need in perl. pointers.
try this:
my $ordinal; if ($foo =~ /(?<!1)1$/) { $ordinal = 'st'; } elsif ($foo =~ /(?<!1)2$/) { $ordinal = 'nd'; } elsif ($foo =~ /(?<!1)3$/) { $ordinal = 'rd'; } else { $ordinal = 'th'; }
Comments
Post a Comment