I want to count the number of uppercase letters in a string using perl.
For example: I need to know how many uppercase characters the word "EeAEzzKUwUHZws" contains.
Beware of Unicode, as the direct thing AZ is not very portable for other characters, such as accented capital letters. if you need to process them too, try:
my $result = 0; $result++ while($string =~ m/\p{Uppercase}/g);
Use the tr operator:
tr
$upper_case_letters = $string =~ tr/AZ//;
This is a general question, and the tr operator is usually superior to other methods .
sub count { $t = shift; $x = 0; for( split//,$t ) { $x++ if m/[AZ]/; } return $x; }
Single line method:
$count = () = $string =~ m/\p{Uppercase}/g
This is based on Stuart Watt's answer , but modified according to the advice that ysth posted in the comments to make it single-line.
Source: https://habr.com/ru/post/892506/More articles:IWordBreaker custom sample for SQL Server 2008 R2 in any language - c ++How can I make platinum characters without spaces Ruby 1.8? - functionHow to programmatically read the value of an attached dependency property? - c #Braintree Revenue Generation - ruby-on-railsTransparent WPF ListBox with selectable items - c #WCF client causes server to hang until connection fails - performanceActiveAdmin - how to display default template in custom action - ruby-on-railsEnable / disable multiple monitors through the Win32 API or the NVidia API? - windows-7Active Rendering Edit Page - validationIs it possible to create model classes using Doctrine 2 directly from the database? - phpAll Articles