counting different number of characters

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chungku
    New Member
    • Jan 2010
    • 4

    #1

    counting different number of characters

    Code:
    #/usr/bin/perl 
     
     use strict;
     use warnings;
      
     
     #  Setup the counters
     
     my $shed_counter= 0;          # question mark counter
     my $doubleshed_counter = 0;      # period counter
     my $ga_counter = 0;    # comma counter
      
      
     # Open a file
     
     open(FILE, "world affairs.txt");
      
     
     # Process the file
     
     while(<FILE>)
     {
         if( $_ = m/\།/)
         {
             $shed_counter++;
         }
      
         if( $_ = m/\།+/)
         {
             $doubleshed_counter++;
         }
      
         if( $_ = m/\ག/)
         {
             $ga_counter++;
         }
     }
      
     print("Number of question marks:  $qm_counter\n");
     print("Number of commas:  $comma_counter\n");
     print("Number of periods:    $period_counter\n");
    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
    ག
    Last edited by numberwhun; Jan 28 '10, 05:55 PM. Reason: Please use code tags!
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    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_cou nter 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.

    Comment

    Working...