Broad Network


String Length and Character Counting in Perl

Handling Strings in Perl – Part 2

Perl Course

Foreword: In this part of the series, I talk about string length and the counting of string characters in different circumstances.

By: Chrysanthus Date Published: 23 Oct 2015

Introduction

This is part 2 of my series, Handling Strings in Perl. In this part of the series, I talk about string length and the counting of string characters in different circumstances. You should have read the previous part of the series before reaching here, as this is a continuation.

String Length
The Perl length() function returns the number of bytes (characters) in a string. Try the following code:

use strict;

    my $str = "This is a string. Ha ha ha";
    my $len = length($str);
    print $len;

The output is 26. If you count the number of characters in the string, the number, 26 will be confirmed.

Counting the Number of Occurrences of a Substring
You can count the number of occurrences of a substring within a string. Now a regex with the g modifier will return a list of substring matches. Also, a list assigned to a scalar variable returns the number of its elements. So the syntax is:

    my $count = () = $string =~ /$substring/g;

The binding operator returns a list (in list context) to fill the empty list. After the empty list acquires elements, it returns it’s number of elements (in scalar context). Remember, the assignment operator is right associative (performing the right-most assignment before the one next to its left, and so on).

Try the following code:

use strict;

    my $subStr = 'cat';
    my $str = "A cat is an animal. A cat is a creature.";

    my $count = () = $str =~ /$subStr/g;

    print $count;

The out put is 2, for number of “cat”

To make the count case insensitive, just use the i modifier.

The transliteration operator, tr/// returns the number of characters replaced or deleted. If you just want to count the number of a particular character, then use the tr/// operator, replacing with nothing. The syntax is:

    my $count = $string =~ tr/X//

where X is the character counted. Try the following code for lowercase w.

use strict;

    my $subStr = 'cat';
    my $str = "We like women in the water.";

    my $count = $str =~ tr/w//;

    print $count;

The output is 2.

To make the count case insensitive, just use the lower and upper case of the character, as in tr/wW//.

Counting the Number of Words
You can count the number of words in a string. The regex is, /\w+/g. \w is the set, [0-9a-zA-Z_] .Try the following code:

use strict;

    my $str = "I love music. More than 25k music albums in the city.";

    my $count = () = $str =~ /\w+/g;

    print $count;

The output is 11.

To count only numbers, use the regex, /[\d\.\d]+/g . Try the following code:

use strict;

    my $str = "The numbers are 5, 256, 23.4 and 0.";

    my $count = () = $str =~ /[\d\.\d]+/g;

    print $count;

The output is 4.

To count words without digits, use the regex, /\b[a-zA-Z_]+\b/g . Try the following code:

use strict;

    my $str = "I love music. More than 25k music albums in the city.";

    my $count = () = $str =~ /\b[a-zA-Z_]+\b/g;

    print $count;

The output is 10.

To count non-word characters, use the regex, /\W+/g . Try the following code:

use strict;

    my $str = "What is this: //**? And what is this: 5647?";

    my $count = () = $str =~ /\W+/g;

    print $count;

The output is, 8. The space is neither a word nor a non-word character; but it is a character.

That is it for this part of the series. We stop here and continue in the next part.

Chrys

Related Links

Perl Basics
Perl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course

BACK NEXT

Comments

Become the Writer's Fan
Send the Writer a Message