473,399 Members | 3,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

counting different number of characters

Expand|Select|Wrap|Line Numbers
  1. #/usr/bin/perl 
  2.  
  3.  use strict;
  4.  use warnings;
  5.  
  6.  
  7.  #  Setup the counters
  8.  
  9.  my $shed_counter= 0;          # question mark counter
  10.  my $doubleshed_counter = 0;      # period counter
  11.  my $ga_counter = 0;    # comma counter
  12.  
  13.  
  14.  # Open a file
  15.  
  16.  open(FILE, "world affairs.txt");
  17.  
  18.  
  19.  # Process the file
  20.  
  21.  while(<FILE>)
  22.  {
  23.      if( $_ = m/\།/)
  24.      {
  25.          $shed_counter++;
  26.      }
  27.  
  28.      if( $_ = m/\།+/)
  29.      {
  30.          $doubleshed_counter++;
  31.      }
  32.  
  33.      if( $_ = m/\ག/)
  34.      {
  35.          $ga_counter++;
  36.      }
  37.  }
  38.  
  39.  print("Number of question marks:  $qm_counter\n");
  40.  print("Number of commas:  $comma_counter\n");
  41.  print("Number of periods:    $period_counter\n");
  42.  
so this three are the charaters i want to count from the input text (which is the whole text )

i want to count the follow three characters
1)།
2)ག
)།།
is it possible??

thanks
chungku
Jan 28 '10 #1
1 1912
chaarmann
785 Expert 512MB
Yes, it's possible.

But your characters are shown all as boxes in this listing, so nobody can verify if you are using the right regular expression. You are writing about question marks, commas and periods, but these characters are definitively no question marks, commas and periods, else we could see them here properly. For example, I can see the comma after "FILE" properly, and the period after "affairs", in line 16: open(FILE, "world affairs.txt")

Other programmers probably wouldn't see the characters correctly, too, only if they use the exact editor and local settings you used.
So do us an the others a favor and code professionally:
use the normal ascii-characters instead of the same-looking national character, and if you want to count some national characters, use hexadecimal values of unicode for them to code them, instead of pasting them directly into the source code.

Example:
You can match hex characters with \x. Just put \x before the hexadecimal number. For instance, if you wish to replace all backspace characters with nothing, use this:

perl -pi -e "s|\x08||g" file.txt


By the way, why are you putting your counting results in $shed_counter, $doubleshed_counter and $ga_counter, but printing out $comma_counter, $period_counter and $qm_counter? all these latter variables are global and not initialized! It looks like a programming error. You probably want to print out the first mentioned variables.
Jan 29 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Srinath Avadhanula | last post by:
Hello, I am wondering if there is a way of counting graphemes (or glyphs) in python. For example, in the following string: u'\u0915\u093e\u0915' ( or equivalently, u"\N{DEVANAGARI LETTER...
3
by: Megan | last post by:
hi everybody- i'm having a counting problem i hope you guys and gals could give me some help with. i have a query that retrieves a bevy of information from several different tables. first let...
18
by: ChadDiesel | last post by:
I appreciate the help on this group. I know I've posted a lot here the last couple of weeks, but I was thrown into a database project at my work with very little Access experience. No other...
7
by: sathyashrayan | last post by:
Group, Following function will check weather a bit is set in the given variouble x. int bit_count(long x) { int n = 0; /* ** The loop will execute once for each bit of x set,
5
by: Matt | last post by:
Alright, so I'm a little confused here...what exactly does this do? I've run it and it doesn't display anything, so I've typed some things into it, to see if it'd do something then, but to no...
1
by: j | last post by:
Hi, I've been trying to do line/character counts on documents that are being uploaded. As well as the "counting" I also have to remove certain sections from the file. So, firstly I was working...
9
by: tman88g | last post by:
I've been trying to learn arrays, and I was wondering how one would go about making a program that would take from the user as many single digit numbers (0 - 9) as the user wants to enter, and then...
4
by: bigbagy | last post by:
Notes The programs will be compiled and tested on the machine which runs the Linux operating system. V3.4 of the GNU C/C++ compiler (gcc ,g++) must be used. A significant amount coding is...
7
by: peraklo | last post by:
Hello, there is another problem i am facing. i have a text file which is about 15000 lines big. i have to cut the last 27 lines from that file and create a new text file that contans those 27...
8
by: xiaolim | last post by:
i making a simple program to count the different kinds of characters in a text file and then display them out, however i only manage to count the total numbers of characters. #include...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.