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

Regex - pattern matching

Expand Messages
  • dmaltez2002
    Hello everyone, I don t know too much about perl programming so please excuse me if this is a dumb question. I m writing a program that reads two files. Each
    Message 1 of 3 , Oct 31, 2002
      Hello everyone,
      I don't know too much about perl programming so please excuse me if
      this is a dumb question.
      I'm writing a program that reads two files. Each file has 2 or more
      lines. I'm trying to get a pattern that would search for words that
      are found in the two files and prints them out ot an output file
      (i.e. file1 "The book got wet when it fell."; file2 "When she closed
      the door, it broke.". I should find the words "the", "when"
      and "it".)

      I'm able to extract all the words from file2 and I can't extract the
      words from fil1, but I'm not sure how to compare the two files and
      extract the common words.

      This is what I have so far.

      open(INPUT1, '<origfile.org') || die ("Error $!\n");
      open(INPUT2, '<patternfile.dat') || die ("Error $!\n");
      open(OUTPUT, '>outputfile.out') || die ("Error $!\n");;

      while ($secondtext = <INPUT2>)
      {
      $secondtext =~ m/\b(\S+)\b/ig;
      $sectemp = $1; # if I put this it just extracts the first match

      while ($origtext = <INPUT1>)
      {
      $origtext =~ m/$secondtext/g;
      $origtemp = $1;
      if ($sectemp eq $origtemp)
      {
      print "the word $sectemp was found \n";
      }
      }
      }

      I'm completely lost. Can you give me some hints.
      Thanks,
      Maltez
    • Hans Ginzel
      ... Hello you, ... So learn more :-) perldoc perl, perldoc perldata, perldoc perlfaq,... ... It is a homework? ... You can bild a hash of words (perldoc
      Message 2 of 3 , Nov 1, 2002
        On Thu, Oct 31, 2002 at 10:49:15PM -0000, dmaltez2002 wrote:
        > Hello everyone,

        Hello you,

        > I don't know too much about perl programming so please excuse me if

        So learn more :-) perldoc perl, perldoc perldata, perldoc perlfaq,...

        > I'm writing a program that reads two files. Each file has 2 or more
        > lines. I'm trying to get a pattern that would search for words that
        > are found in the two files and prints them out ot an output file
        > (i.e. file1 "The book got wet when it fell."; file2 "When she closed
        > the door, it broke.". I should find the words "the", "when"
        > and "it".)

        It is a homework?

        > I'm able to extract all the words from file2 and I can't extract the
        > words from fil1, but I'm not sure how to compare the two files and
        > extract the common words.

        You can bild a hash of words (perldoc perldata) from each file

        ($file_1, $file_2) = @ARGV; # filenames from commandline

        open F, $file_1 or die "Cannot open file `$file_1': $!";
        while (<F>) {
        $hash_1{$_}++ foreach split /\W+/;
        }
        close F;
        delete $hash_1{""};

        open F, $file_2 or die "Cannot open file `$file_2': $!";
        while (<F>) {
        $hash_2{$_}++ foreach split /\W+/;
        }
        close F;
        delete $hash_2{""};

        $\="\n"; # perdoc perlvar
        print foreach grep { $hash_2{$_} } keys %hash_1;
        # perldoc -f grep; perldoc -f keys


        > I'm completely lost. Can you give me some hints.

        I hope you are able to read manuals and FAQs.

        perldoc perdata, perldoc perlfaq4

        How can I get the unique keys from two hashes?

        Now you cna write to perldoc maintainers an answer to question

        How can I get the intersection keys from two hashes?

        perldoc -f exists


        Or thing about efficiency and write a better code.

        Hans
      • dmaltez2002
        Nevermind. I figured out. That s what I m doing: learning and reading and testing. That s why I asked for advise :-). I m sure that when I m done with the
        Message 3 of 3 , Nov 1, 2002
          Nevermind. I figured out.
          That's what I'm doing: learning and reading and testing. That's why I
          asked for advise :-). I'm sure that when I'm done with the first few
          chapters of my Perl book I would know enough to write good codes.
          I'll check out the perldocs as you suggested.
          Thanks for you help Hans.
          Maltez

          --- In perl-beginner@y..., Hans Ginzel <hans@m...> wrote:
          >
          > On Thu, Oct 31, 2002 at 10:49:15PM -0000, dmaltez2002 wrote:
          > > Hello everyone,
          >
          > Hello you,
          >
          > > I don't know too much about perl programming so please excuse me
          if
          >
          > So learn more :-) perldoc perl, perldoc perldata, perldoc
          perlfaq,...
          >
          > > I'm writing a program that reads two files. Each file has 2 or
          more
          > > lines. I'm trying to get a pattern that would search for words
          that
          > > are found in the two files and prints them out ot an output file
          > > (i.e. file1 "The book got wet when it fell."; file2 "When she
          closed
          > > the door, it broke.". I should find the words "the", "when"
          > > and "it".)
          >
          > It is a homework?
          >
          > > I'm able to extract all the words from file2 and I can't extract
          the
          > > words from fil1, but I'm not sure how to compare the two files and
          > > extract the common words.
          >
          > You can bild a hash of words (perldoc perldata) from each file
          >
          > ($file_1, $file_2) = @ARGV; # filenames from
          commandline
          >
          > open F, $file_1 or die "Cannot open file `$file_1': $!";
          > while (<F>) {
          > $hash_1{$_}++ foreach split /\W+/;
          > }
          > close F;
          > delete $hash_1{""};
          >
          > open F, $file_2 or die "Cannot open file `$file_2': $!";
          > while (<F>) {
          > $hash_2{$_}++ foreach split /\W+/;
          > }
          > close F;
          > delete $hash_2{""};
          >
          > $\="\n"; # perdoc perlvar
          > print foreach grep { $hash_2{$_} } keys %hash_1;
          > # perldoc -f grep; perldoc -f keys
          >
          >
          > > I'm completely lost. Can you give me some hints.
          >
          > I hope you are able to read manuals and FAQs.
          >
          > perldoc perdata, perldoc perlfaq4
          >
          > How can I get the unique keys from two hashes?
          >
          > Now you cna write to perldoc maintainers an answer to question
          >
          > How can I get the intersection keys from two hashes?
          >
          > perldoc -f exists
          >
          >
          > Or thing about efficiency and write a better code.
          >
          > Hans
        Your message has been successfully submitted and would be delivered to recipients shortly.