Perl counts the number of occurrences of specific characters in a string

Source: Internet
Author: User

Perl counts the number of occurrences of specific characters in a string
I recently found some related skills from the Internet when I encountered a problem in programming, as follows:
(The array and hash methods are no longer listed)
Method 1:

 #!/usr/bin/perl -w use strict;  my $str = 'abcsfsaf#sdagasdga#sdgaghoiiopjh#dsfjkopahkl;fjh#dsjfklpafj '; my $count = $str =~ tr/#/#/; 

Counts the number of # In the string $ Str. Here, the conversion operator tr // is used, which is simple and fast.

The difference between S // and TR // is attached:
S //: Replacement operator. S/searchpattern/Repalcement/; by default, $ _ is searched to find the searchpattern and replacement is used to replace the entire matched regular expression. This operator returns the number of matches or the number of replicas. If no matches are performed, 0 is returned.
You can also specify a variable, such as $ Perl = ~ S/searchpattern/Repalcement/; in addition, the replacement operator can also use a non-slash (/) delimiter, such as S ###.
Tr //: conversion operator. Tr // is similar to the replacement operator, but it does not use a regular expression, and its running mode is completely different. The syntax of the conversion operator is as follows: TR/searchment/Repalcement/; Replace the first character of searchment with the first character of replacement, and change the second character of searchment to the second character of replacement, and so on ~~~~~~~
That is, the conversion operator t r // is used to search for a string, find all the elements in the searchlist, and replace them with the corresponding elements in the replacementlist. If r e p l a c e m e n t l I s t is null, or is the same as s e a r c h l I s t, then t r // will calculate and return matched characters. The target string is not modified.
 

Method 2: 

My $ Var = 'tctcatgtgaaaaactatatcaataataaaaaca '; my $ COUNT = ($ Var = ~ S/A/G); print $ count; # Calculate the character in the string $ var, and use the regular expression to match S // G.

 
Method 3:

My $ STR = 'abcsfsaf # sdagasdga # sdgaghoiioploud # dsfjkopahkl; fstrap # dsjfklpafj '; my $ cc = 0; my $ TMP = 0; if ($ TMP = () = ($ STR = ~ /#/G) {$ CC + = $ TMP;} print "$ CC \ n"; Calculate the # number in the string $ STR, and use regular expression matching. This method can also be written as follows, which is more concise. #! /Usr/bin/perl # Find. plmy $ s; $ S + = () =/A/G) while <>; print "A: $ s \ n ";

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.