469,362 Members | 2,295 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,362 developers. It's quick & easy.

Punctuation characters

Hi,
how can I count the number of punctuation characters within a text file prompted from the keyboard?
Sep 10 '06 #1
12 3827
Hi,
how can I count the number of punctuation characters within a text file prompted from the keyboard?
suppose you have to search ! in the given file regexpr.pl then please refer below code

#!/usr/bin/perl;

open(fp1,"regexpr.pl") || die "no";

my @array=<fp1>;

close(fp1);

my $count;

my $temp;

$count=0;

$temp=0;

foreach $str (@array)

{

$_=$str;

$temp= tr/!/!/;

$count=$count+$temp;

}

print "$count\n";




actually when you try to #!/usr/bin/perl;

open(fp1,"regexpr.pl") || die "no";

my @array=<fp1>;

close(fp1);

my $count;

my $temp;

$count=0;

$temp=0;

foreach $str (@array)

{

$_=$str;

$temp= tr/!/!/;

$count=$count+$temp;

}

print "$count\n";

tr will replace 1 character with anonther and then return the number of time replacement happened.

here I am replaceing ! with ! so after replacement it will given you actual count of !

let me know if this can solve your problem.
Sep 28 '06 #2
suppose you have to search ! in the given file regexpr.pl then please refer below code

#!/usr/bin/perl;

open(fp1,"regexpr.pl") || die "no";

my @array=<fp1>;

close(fp1);

my $count;

my $temp;

$count=0;

$temp=0;

foreach $str (@array)

{

$_=$str;

$temp= tr/!/!/;

$count=$count+$temp;

}

print "$count\n";




actually when you try to #!/usr/bin/perl;

open(fp1,"regexpr.pl") || die "no";

my @array=<fp1>;

close(fp1);

my $count;

my $temp;

$count=0;

$temp=0;

foreach $str (@array)

{

$_=$str;

$temp= tr/!/!/;

$count=$count+$temp;

}

print "$count\n";

tr will replace 1 character with anonther and then return the number of time replacement happened.

here I am replaceing ! with ! so after replacement it will given you actual count of !

let me know if this can solve your problem.

Mistakenly i have pasted the code twice
Sep 28 '06 #3
Hi,
thanks for your code.I was trying to run it but always returning an error message that the file couldn't be opened at C:\Perl\perlscripts\test.pl.I placed the tested file to the same directory as Perl is located.Where have I gone wrong?
Oct 7 '06 #4
Hi,
thanks for your code.I was trying to run it but always returning an error message that the file couldn't be opened at C:\Perl\perlscripts\test.pl.I placed the tested file to the same directory as Perl is located.Where have I gone wrong?
#########

the problem is with file path.

check the file you have given in the script.

if you are running the script from the location where the data file is kept then there is no need even to specify path since it searched the file in current working directory.
Oct 9 '06 #5
#########

the problem is with file path.

check the file you have given in the script.

if you are running the script from the location where the data file is kept then there is no need even to specify path since it searched the file in current working directory.
Hi,
I checked the file and it seems everything all right although it doesn,t work.The error message is:1 at C:\Perl\perlscripts\Countext.pl line 2.
Couldn't open this file.
Oct 9 '06 #6
Hi,
I checked the file and it seems everything all right although it doesn,t work.The error message is:1 at C:\Perl\perlscripts\Countext.pl line 2.
Couldn't open this file.

can you please paste the code for opening file that you are using...

it will help me in debugging.

Also send some sample data
Oct 10 '06 #7
can you please paste the code for opening file that you are using...

it will help me in debugging.

Also send some sample data
i think i got problem script

lets think that your file is at perl directory under C: drive

you need to put this path as below:
c:/perl/test.pl
and not like c:\perl\test.pl

because '/' is the path seperator on windows
Oct 10 '06 #8
i think i got problem script

lets think that your file is at perl directory under C: drive

you need to put this path as below:
c:/perl/test.pl
and not like c:\perl\test.pl

because '/' is the path seperator on windows
Hi,

#!/usr/local/bin/perl
open (FILEHANDLE, school.txt) || die (print"Couldn't open this file");

This is a code I used.I put the path as you advised but it asks for backslash and returning the same output.File school.txt is in Perl directory.
Oct 11 '06 #9
Hi,

#!/usr/local/bin/perl
open (FILEHANDLE, school.txt) || die (print"Couldn't open this file");

This is a code I used.I put the path as you advised but it asks for backslash and returning the same output.File school.txt is in Perl directory.
first of all if you are using above code as it is then it need to be as follow
open (FILEHANDLE, "school.txt") || die "Couldn't open this file";
or if it is in perl direcotry
open (FILEHANDLE, "c:/perl/school.txt") || die "Couldn't open this file";

try it out and let me know
regards,
dipak
Oct 11 '06 #10
first of all if you are using above code as it is then it need to be as follow
open (FILEHANDLE, "school.txt") || die "Couldn't open this file";
or if it is in perl direcotry
open (FILEHANDLE, "c:/perl/school.txt") || die "Couldn't open this file";

try it out and let me know
regards,
dipak
Hi,
I tried it.The result was: Couldn't open this file at C:\Perl\perlscripts\Countext.pl
Maybe I typed something wrong in Command Prompt.Well,the script and the text file is on the above-mentioned path.
In Command Prompt I typed-C:\ Perl>perlscripts\Countext.pl.
regards,
Zoltan
Oct 11 '06 #11
geek491
21
can you please paste the code for opening file that you are using...

it will help me in debugging.

Also send some sample data
Hi guys

The error message is:1 at C:\Perl\perlscripts\Countext.pl line 2.
Couldn't open this file.

is because of the \ special character.. to nullify it... you need to do this

open(FILEHANDLE, "C:\\Perl\\perlscripts\\Countext.pl");

now your code will run fine. assuming user inputs this file name from command prompt then you need to make sure user enters two \ like \\


or else u can use regular expression and substitute \ with \\ and then open the file...

happy programming :)

let me know on how this helped you to geek491@yahoo.co.in
Oct 13 '06 #12
miller
1,089 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. my $inFile = $ARGV[0] or die "no file specified";
  4.  
  5. open(IN, $inFile) or die "open $inFile: $!";
  6.  
  7. my $count = 0;
  8.  
  9. while (my $line = <IN>) {
  10.     while ($line =~ m{([\.\!\?])}g) {
  11.         print "$1";
  12.         $count++;
  13.     }
  14. }
  15.  
  16. print "\n$count punctuation characters in $inFile\n";
  17.  
  18. close(IN) or die "close $inFile: $!";
  19.  
  20. 1;
  21.  
  22. __END__
  23.  
The above script takes in a single file as a parameter and then outputs all the punctuation characters. You'll have to edit the regular expression to list all the characters that you wish to count. I currently have escaped them all, although in truth the only ones that you truly need to escape are a carat "^" if you list it as the first charater, a bracket "]" as that will close the charater class, or a slash "\" as that will simply say the next character is a literal. Just to be safe though, that is why it is easier just to escape all punctuation characters used in a character class so that you can be sure they are all treated as literals.
Oct 16 '06 #13

Post your reply

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

Similar topics

5 posts views Thread by HumanJHawkins | last post: by
2 posts views Thread by masood.iqbal | last post: by
7 posts views Thread by Lachlan Hunt | last post: by
reply views Thread by Chris Leffer | last post: by
4 posts views Thread by kdsutaia | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.