Reputation: 169
I am a beginner in Perl. I want to display the Tax Types of different countries for my new website in Perl. Based on the respective countries from the API I use, I get all the Tax Types in Uppercase letters. I don’t want all in uppercase letters I have tried the following regular expression. Following is the code:
my $key = <input>;
my $value = $key =~ /^(\w\w\w? \w)(.+)$/ ? $1 . lc $2 : ucfirst lc $key;
print $value;
Here are inputs and outputs that I get from the above code:
1: Input: ‘AUSTRALIA GST’
Output: ‘Australia gst’
2. Input: ‘AZ COUNTY TAX’
Output: ‘AZ County tax’
3. Input: ‘NEW ZEALAND GST’
Output: ‘NEW Zealand gst’
4. Input: ‘VAT’
Output: ‘Vat’
I want the following outputs for the respective inputs from the same single code:
1: Input: ‘AUSTRALIA GST’
Output: ‘Australia GST’
2. Input: ‘AZ COUNTY TAX’
Output: ‘AZ County Tax’
3. Input: ‘NEW ZEALAND GST’
Output: ‘New Zealand GST’
4. Input: ‘VAT’
Output: ‘VAT’
Can anyone please help as to how to dynamically fix this with regular expression in PERL ?
Upvotes: 0
Views: 66
Reputation: 39158
use 5.016;
sub maybe_ucfirst {
my ($word) = @_;
my %ignore = map { $_ => undef } qw(GST AZ VAT);
return exists $ignore{$word} ? $word : ucfirst fc $word;
}
my @inputs = ('AUSTRALIA GST', 'AZ COUNTY TAX', 'NEW ZEALAND GST', 'VAT');
for my $s (@inputs) {
$s =~ s/(\w+)/maybe_ucfirst($1)/eg;
say $s;
}
__END__
Australia GST
AZ County Tax
New Zealand GST
VAT
Upvotes: 3