in reply to Getting abbreviations or initials
Three small suggestions, lightly tested:
#$name =~ s/^(The|A|An) //i; $name =~ s/^(?:The|An?) //i; # no capturing #for my $word (split(/[ _-]/,$name)) { # push @abbr, substr($word,0,1); #} my @abbr = $name =~ /(?:_|\b)(\w)/g; #my $raw_abbr = $opt{periods} && $opt{periods} =~ /^[yt1]/i ? join(' +',map { $_ =~ s/$/./; $_; } @abbr) : join('',@abbr); my $raw_abbr = $opt{periods} && $opt{periods} =~ /^[yt1]/i ? join('.',@abbr) . '.' : join('', @abbr);
Update:Thinking about it a bit more, I'd do all the [yt1] testing up front, and eliminate the intermediate variables:
use strict; use warnings; my %opt = ( periods => 0, ALLCAPS => 1, HTML => 1, name => "Compact Disc read-only memory", ); print abbr(%opt); sub abbr { my %opt = @_; $opt{name} =~ s/^(?:The|An?) //i; die("Sorry...") unless $opt{name} =~ /\S/; my %ON = map { $_ => 1 } grep { $opt{$_} =~ /^[yt1]$/i } keys %opt; return ($ON{HTML} ? qq{<abbr title="$opt{name}">} : "") . ( join '', map { $_ = $ON{ALLCAPS} ? uc : $_; $_ = $ON{periods} ? "$_." : $_; } $opt{name} =~ /(?:_|\b)(\w)/g ) . ($ON{HTML} ? qq{</abbr>} : "") }
And confession! Somehow, I did not know this worked:
/(The|A|An)/;
Sadly, I've always wrapped pipes in non-capturing parens, and wrapped it all in capturing parens:
/((?:The|A|An))/;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Getting abbreviations or initials
by Lady_Aleena (Priest) on Aug 20, 2012 at 23:50 UTC | |
by hbm (Hermit) on Aug 21, 2012 at 00:54 UTC |