Realize the word counter with Perl

xiaoxiao2021-03-06  77

Perl's text handling capacity, a simple line of code can realize the count of words.

While (<>) # Standard input does not end

{

While (/ w [/ w -] *) / g) # word match / w represents letters or numbers, g represents matching to the end

{

$ Words {$ 1} ; # has saved data in the hash table Represents the current variable

}

}

Foreach (Keys% Words)

{

Print "$ _ $ words {$ _} / n"; # with foreach output

}

A total of 5 lines of code, plus the braces 10 lines.

Saved to Test.pl, if you installed Perl, you can try it.

test:

C: / perl /> perl test.pl

What is your name?

My name is perl

The ^ z file ends.

Output:

Perl 1

Your 1

What 1

My 1

Name 2

IS 2

C: / perl />

This is to use C language, I don't know how to do it.

转载请注明原文地址:https://www.9cbs.com/read-109092.html

New Post(0)