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

Re: perl program for counting words beween headings in a file

Expand Messages
  • Rex
    You are missing the extraction of the heading in each line. For each pass you should recognize the heading and extract it using parenthesis. Once you have the
    Message 1 of 3 , Jul 29, 2003
    • 0 Attachment
      You are missing the extraction of the heading in each line. For each
      pass you should recognize the heading and extract it using
      parenthesis. Once you have the heading you can count the words (not
      headings) into a hash. The following will do just that.

      ##### Code start
      my %words;
      my $head;

      foreach (@dna) {
      chomp;
      if($_ =~ m!(^\*.*)!) { # found heading, extract into $1
      $head = $1;
      } else { # not a heading, then count it
      $words{$head}++;
      }
      }

      while ( my ($h,$w) = each %words ) {
      print "$h: $w\n";
      }
      ##### code end

      Unless the lines contain actual words and numbers this will work. If
      it does, you need to include another regex within the else to filter
      those out.

      Hope this helps.
      Rex


      --- In perl-beginner@yahoogroups.com, "sravul" <sravul@y...> wrote:
      > Hi
      > I have a file which has a number of headings that start with
      a "*".I
      > need to identify each star and count the number of times each word
      has
      > occured and output the number.I am able to count the words but not
      > sure about recognizing the "*".
      > the file looks like the
      > *365
      > word
      > word
      > word
      > num
      > num
      > wrod
      > *320
      > num
      > num
      > num
      > word
      > *365
      > char
      > char
      > char
      > num
      > Iam able to count the words.But not able to recognize and print
      each
      > heading.
      > I tried to use the following code to recognize the heading.but is
      not
      > working.
      > foreach my $line (@dna) {
      > if($line =~ m/[\*]+/)
      > {
      > $head = $line;
      > print OUT "$head\n";
      > }
      > Any help will be appreciated.
    Your message has been successfully submitted and would be delivered to recipients shortly.