473,320 Members | 2,048 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,320 software developers and data experts.

Counting Punctuation Marks (in a text file)

I want to ask for some advice on a script that will count all the punctuation marks in a text file, I know it's probably quite a basic thing but I am new to Perl and would really appreciate the help, the output would also need to display how many occurances of each there are (such as ?, ., ,, ..., ! -, ;, : e.t.c)

Thanks in advance,

Gary
Aug 14 '07 #1
6 4157
numberwhun
3,509 Expert Mod 2GB
Hi Gary! Welcome!

Please know that when you post to the forum, we really need to see the code that you have tried thus far. That way we can assist you with the issue(s)/problem(s) you are having and can help get your code working.

Also, I don't know about everyone else, but this sounds distinctly like it is a homework assignment. If we wrote it for you, you wouldn't learn a thing. Why don't you give it a shot and see what you come up with and we will guide you from there.

Personally, I would cycle through each line of the file using a while loop, and in the while loop, test each line for the punctuation marks(with a regex). It would also be a good idea to have a counter going. If you need to count each individual punctuation, then you may want multiple counters, but that is just me.

Perl is such a wonderful language that this exercise should show you a few different possibilities.

Regards,

Jeff
Aug 14 '07 #2
Thanks Jeff,

You are right in thinking this looks like a homework assignment.

I have been stuck on this one question for nearly four months now and it is really bugging me.

I have read the Perl folder of my course nearly nine times and it is the counter that I am having the problem with.

I know how to search for any characters, including punctuation marks but not sure on how to return the count for each one, I was think of assigning them to an array and then printing the contents of the array at the end, but its the code for generating a count of each character that is troubling me.

When I get home tonight I will post the code I have created so far, your quick response and help so far is very, very much appreciated... I need to know this because it's the only part of the course I am having problems with, but If I can't do this I can't finish the course and so it's £2,500 down the drain (and hundreds & hundreds of hours of study time I have put in) I don't feel that posting this is cheating though, not really because considering I have read this section of the course nine times, my opinion is that it was not sufficiently covered in my study material - (I can only know what I have learnt!)

Very best regards,

Gary Colman (sv5perl)
Aug 14 '07 #3
numberwhun
3,509 Expert Mod 2GB
Well, once you feed the file into the while loop, you can cycle through with a bunch of if statements for each one. Here is an example. Feel free to use this and come up with something complete.

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 $qm_counter = 0;          # question mark counter
  10. my $period_counter = 0;      # period counter
  11. my $comma_counter = 0;    # comma counter
  12.  
  13.  
  14. ###################################
  15. # Open a file
  16. ###################################
  17. open(FILE, "./myFile.txt");
  18.  
  19. ###################################
  20. # Process the file
  21. ###################################
  22. while(<FILE>)
  23. {
  24.     if( $_ = m/\?/)
  25.     {
  26.         qm_counter++;
  27.     }
  28.  
  29.     if( $_ = m/\,/)
  30.     {
  31.         comma_counter++;
  32.     }
  33.  
  34.     if( $_ = m/\./)
  35.     {
  36.         period_counter++;
  37.     }
  38. }
  39.  
  40. print("Number of question marks:  $qm_counter");
  41. print("Number of commas:  $comma_counter");
  42. print("Number of periods:    $period_counter");
  43.  
  44.  
That should get you started. Granted, as always, TIMTOWTDI, but this isn't that bad and should get you to where you want to go. That will cycle through each line, checking the line for each punctuation listed. If you have others to check for, then just add other instances inside of the while loop.

Regards,

Jeff
Aug 14 '07 #4
KevinADC
4,059 Expert 2GB
You are basically counting sub strings:

how do I count sub strings
Aug 14 '07 #5
miller
1,089 Expert 1GB
Only because ... well ... you know:

Expand|Select|Wrap|Line Numbers
  1. >perl -MData::Dumper -ne "END {print Dumper(\%c)}; $c{$1}++ while (/([^\w\s])/g);" scratch.txt
  2. $VAR1 = {
  3.           ':' => 1,
  4.           ',' => 10,
  5.           '?' => 1,
  6.           '-' => 1,
  7.           ')' => 1,
  8.           '.' => 6,
  9.           '\'' => 1,
  10.           '(' => 1,
  11.           ';' => 1,
  12.           '!' => 1
  13.         };
  14.  
- Miller
Aug 14 '07 #6
thanks kindly for your helpful posting.

Regards,

Gary.
Aug 14 '07 #7

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

Similar topics

7
by: Lachlan Hunt | last post by:
Hi, I have recently downloaded and experemented with IBM HPR 3.0, and Opera 8 with text-to-speech, and have come to realise some fairly annoying issues regarding punctuation marks. I've found,...
1
by: Derek Hart | last post by:
Using VB.NET, I wish to open a text file that will have paragraph marks throughout it. To replace them, I can probably use the Replace command and replace vbcrlf. But can somebody give me sample...
5
by: andy.lee23 | last post by:
hi im having trouble counting lines in a text file, i have the following code int node1, node2, i; char name; float value; ifstream fin; fin.open(OpenDialog1->FileName.c_str()); i=1;
5
by: pihkal23 | last post by:
Hi I am not a PHP user; I manage the server/ network for our business. We've employed web developers (chosen because they're friends of the MD) to build us a site and they've used PHP & MySQL to...
3
by: Avi | last post by:
I need to create a text file that has the data from the 10 tables in the database. The number of fields in the tables exceeds 255 and so I cannot make a new table with all the fields and then...
2
by: Anat | last post by:
Hi, I need a little help on performing string manipulation: I want to take a given string, and make certain words hyperlinks. For example: "Hello world, this is a wonderful day!" I'd like the...
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...
39
by: Umesh | last post by:
Plese help. Is there any software by which we can do that?
6
by: watcher00 | last post by:
Hi I'm a complete newbie at Perl and i was wondering if i can get some help completing an exercise i've come across. I need to count the punctuation marks from a text file and then output a...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.