Punctuation characters

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Limeburner72
    New Member
    • Sep 2006
    • 11

    Punctuation characters

    Hi,
    how can I count the number of punctuation characters within a text file prompted from the keyboard?
  • deep022in
    New Member
    • Sep 2006
    • 23

    #2
    Originally posted by Limeburner72
    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,"regex pr.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,"regex pr.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.

    Comment

    • deep022in
      New Member
      • Sep 2006
      • 23

      #3
      Originally posted by deep022in
      suppose you have to search ! in the given file regexpr.pl then please refer below code

      #!/usr/bin/perl;

      open(fp1,"regex pr.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,"regex pr.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

      Comment

      • Limeburner72
        New Member
        • Sep 2006
        • 11

        #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\perlscr ipts\test.pl.I placed the tested file to the same directory as Perl is located.Where have I gone wrong?

        Comment

        • deep022in
          New Member
          • Sep 2006
          • 23

          #5
          Originally posted by Limeburner72
          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\perlscr ipts\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.

          Comment

          • Limeburner72
            New Member
            • Sep 2006
            • 11

            #6
            Originally posted by deep022in
            #########

            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\perlscr ipts\Countext.p l line 2.
            Couldn't open this file.

            Comment

            • deep022in
              New Member
              • Sep 2006
              • 23

              #7
              Originally posted by Limeburner72
              Hi,
              I checked the file and it seems everything all right although it doesn,t work.The error message is:1 at C:\Perl\perlscr ipts\Countext.p l 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

              Comment

              • deep022in
                New Member
                • Sep 2006
                • 23

                #8
                Originally posted by deep022in
                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

                Comment

                • Limeburner72
                  New Member
                  • Sep 2006
                  • 11

                  #9
                  Originally posted by deep022in
                  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.

                  Comment

                  • deep022in
                    New Member
                    • Sep 2006
                    • 23

                    #10
                    Originally posted by Limeburner72
                    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

                    Comment

                    • Limeburner72
                      New Member
                      • Sep 2006
                      • 11

                      #11
                      Originally posted by deep022in
                      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\perlscr ipts\Countext.p l
                      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>perlscript s\Countext.pl.
                      regards,
                      Zoltan

                      Comment

                      • geek491
                        New Member
                        • Oct 2006
                        • 21

                        #12
                        Originally posted by deep022in
                        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\perlscr ipts\Countext.p l 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\\perl scripts\\Counte xt.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.c o.in

                        Comment

                        • miller
                          Recognized Expert Top Contributor
                          • Oct 2006
                          • 1086

                          #13
                          Code:
                          #!/usr/bin/perl
                          
                          my $inFile = $ARGV[0] or die "no file specified";
                          
                          open(IN, $inFile) or die "open $inFile: $!";
                          
                          my $count = 0;
                          
                          while (my $line = <IN>) {
                          	while ($line =~ m{([\.\!\?])}g) {
                          		print "$1";
                          		$count++;
                          	}
                          }
                          
                          print "\n$count punctuation characters in $inFile\n";
                          
                          close(IN) or die "close $inFile: $!";
                          
                          1;
                          
                          __END__
                          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.

                          Comment

                          Working...