http://www.perlmonks.org?node_id=11105595


in reply to Re: Match text from txt to html
in thread Match text from txt to html

Hi,

Thank you for your reply.

To be honest, I have no idea... I know Perl but I don't know where to start.

What I did, was to read the TXT and store it in an array, then I read line by line the HTML and match the sentences without tags.

Replies are listed 'Best First'.
Re^3: Match text from txt to html
by talexb (Chancellor) on Sep 04, 2019 at 14:16 UTC

    Great! And is the code working correctly? (Quietly loads the confetti cannon.)

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

      this is my code as of now.

      #!/usr/bin/perl use warnings; my $html = shift; my $htmlvar; open(my $fh, '<', $html) or die "cannot open file $html"; { chomp; local $/; $htmlvar = <$fh>; } close($fh); my $line = 1; while (<STDIN>){ chomp; if ($htmlvar =~ /$_/ && $htmlvar !~ /<sentence id\"[0-9]*\">$_<\/ +sentence>/){ $htmlvar =~ s/($_)/<sentence id\"$line\">$1<\/sentence>/; $line++ } } print "$htmlvar\n";

        When open fails, get it to tell you why:

        open(my $fh, '<', $html) or die "cannot open file $html: $!";

        Trying to parse HTML like this is more often than not a waste of time.

      Thanks! I am opening the html this way as I don't know the nodes of the files. the created automatically, so this is why I am reading them as text.