Loading ...
Sorry, an error occurred while loading the content.

Count and Replace definite articles and indefinite articles

Expand Messages
  • zhshqzyc
    I used a function to count and replace articles and indefinite articles in a pragraph. This pragraph is in a text file. What is wrong? perl aa.pl bb.dat [code]
    Message 1 of 3 , Nov 5, 2006
    • 0 Attachment
      I used a function to count and replace articles and indefinite
      articles in a pragraph. This pragraph is in a text file.
      What is wrong?

      perl aa.pl bb.dat

      [code]
      use English;

      my $def_count; # the number of definite articles
      my $indef_count; # the number of indefinite articles

      sub count {
      if ( $ARG[0] =~ /the/ ) #An expression that counts as true if the
      lowercase version of the only argument of count is equal to "the"
      {
      $def_count++;
      }
      else {
      $indef_count++;
      }

      return /uc($ARG[0])/;#return the uppercase of the only argument of
      count.
      }

      while ( <> ) {
      s/\b(an|a)\b/count($1)/egi;
      print "$ARG[0]";#output the line to which the substitution
      expression has just been applied
      }

      print "\n\nThere were $def_count definite articles\n",
      "and $indef_count indefinite articles in the text.\n";
      [/code]
    • Damien Carbery
      ... What errors are you getting? What debugging have you done? ... You should initialise these variables, probably set them to 0. ... Why are you using the
      Message 2 of 3 , Nov 6, 2006
      • 0 Attachment
        --- In perl-beginner@yahoogroups.com, "zhshqzyc" <zhshqzyc@...> wrote:
        >
        > I used a function to count and replace articles and indefinite
        > articles in a pragraph. This pragraph is in a text file.
        > What is wrong?
        >
        > perl aa.pl bb.dat
        >
        > [code]
        > use English;
        >
        > my $def_count; # the number of definite articles
        > my $indef_count; # the number of indefinite articles
        >
        > sub count {
        > if ( $ARG[0] =~ /the/ ) #An expression that counts as true if the
        > lowercase version of the only argument of count is equal to "the"
        > {
        > $def_count++;
        > }
        > else {
        > $indef_count++;
        > }
        >
        > return /uc($ARG[0])/;#return the uppercase of the only argument of
        > count.
        > }
        >
        > while ( <> ) {
        > s/\b(an|a)\b/count($1)/egi;
        > print "$ARG[0]";#output the line to which the substitution
        > expression has just been applied
        > }
        >
        > print "\n\nThere were $def_count definite articles\n",
        > "and $indef_count indefinite articles in the text.\n";
        > [/code]
        >
        What errors are you getting?
        What debugging have you done?

        > my $def_count; # the number of definite articles
        > my $indef_count; # the number of indefinite articles
        You should initialise these variables, probably set them to 0.

        > return /uc($ARG[0])/;
        Why are you using the forward slashes?

        > s/\b(an|a)\b/count($1)/egi;
        You are not searching for 'the' so $def_count will never be incremented.

        > print "$ARG[0]";#output the line to which the substitution
        $ARG[0] exists inside the count() function, not outside it. You need
        to use another variable here.

        I made 4 small changes (hint: see my comments/questions above) and it
        now counts definite and indefinite articles in the paragraph.
      • zhshqzyc
        ... OK!LET ME TRY. (1)initialise these variables are unneceesary. (2)return $ARG[0]); (3)s/ b(the|an|a) b/count($1)/egi; (4)print $_ ;
        Message 3 of 3 , Nov 6, 2006
        • 0 Attachment
          --- In perl-beginner@yahoogroups.com, "Damien Carbery" <daymobrew@...>
          wrote:
          >
          > --- In perl-beginner@yahoogroups.com, "zhshqzyc" <zhshqzyc@> wrote:
          > >
          > > I used a function to count and replace articles and indefinite
          > > articles in a pragraph. This pragraph is in a text file.
          > > What is wrong?
          > >
          > > perl aa.pl bb.dat
          > >
          > > [code]
          > > use English;
          > >
          > > my $def_count; # the number of definite articles
          > > my $indef_count; # the number of indefinite articles
          > >
          > > sub count {
          > > if ( $ARG[0] =~ /the/ ) #An expression that counts as true if the
          > > lowercase version of the only argument of count is equal to "the"
          > > {
          > > $def_count++;
          > > }
          > > else {
          > > $indef_count++;
          > > }
          > >
          > > return /uc($ARG[0])/;#return the uppercase of the only argument of
          > > count.
          > > }
          > >
          > > while ( <> ) {
          > > s/\b(an|a)\b/count($1)/egi;
          > > print "$ARG[0]";#output the line to which the substitution
          > > expression has just been applied
          > > }
          > >
          > > print "\n\nThere were $def_count definite articles\n",
          > > "and $indef_count indefinite articles in the text.\n";
          > > [/code]
          > >
          > What errors are you getting?
          > What debugging have you done?
          >
          > > my $def_count; # the number of definite articles
          > > my $indef_count; # the number of indefinite articles
          > You should initialise these variables, probably set them to 0.
          >
          > > return /uc($ARG[0])/;
          > Why are you using the forward slashes?
          >
          > > s/\b(an|a)\b/count($1)/egi;
          > You are not searching for 'the' so $def_count will never be incremented.
          >
          > > print "$ARG[0]";#output the line to which the substitution
          > $ARG[0] exists inside the count() function, not outside it. You need
          > to use another variable here.
          >
          > I made 4 small changes (hint: see my comments/questions above) and it
          > now counts definite and indefinite articles in the paragraph.
          OK!LET ME TRY.

          (1)initialise these variables are unneceesary.
          (2)return $ARG[0]);
          (3)s/\b(the|an|a)\b/count($1)/egi;
          (4)print "$_";
        Your message has been successfully submitted and would be delivered to recipients shortly.