Perl program

dragnyon -  
[Dal] Posted messages 6122 Registration date   Status Contributor Last intervention   -
Hello,
I need your help with my project in Perl (you can see the topic and the programs I've made here https://transfernow.net/02bfp7o17g7c), I managed to complete exo1.pl but I'm stuck on exo2.pl, the program gives me page22.html, 624 while I should find page22.html 201. I've been working on this for hours without finding the solution.
Thank you in advance for your help.

4 answers

dragnyon
 
up
0
dragnyon
 
up!
0
[Dal] Posted messages 6122 Registration date   Status Contributor Last intervention   1 108
 
Hello dragnyon,

If you want better chances of getting help quickly, you need to make it easier for the forum members instead of sending them to an online archive with nearly 30 files, without any explanations about what the code is supposed to do and with what.

In the future, post a minimal reproducible code with test data illustrating your problem, instead of forcing the reader, who may not have the time or motivation, to go fishing for information.

So, after doing a bit of exploration, here is the code you wrote for your exo2.pl:

#! /usr/bin/perl use strict; use warnings; { my ($fich, $mot) = @ARGV; my $ligne; my $lec; my $nb=0; my $nbmot=0; open ($lec,"<",$fich) or die "error on $fich"; while (<$lec>) { $ligne=$_; if ($ligne=~ m/[ >]$mot[s]?[ \.\;\,\:<]/i) {$nb++; if($ligne=~ m/[<title<|<\/title>]/) {$nb=$nb+3; } if($ligne=~ m/href/) {$nb=$nb+2; } } } close($lec); print "$fich, $nb"; }


Your indentation is not clear, but one of the issues with your exo2.pl script is that you are looking for the presence of the title and href tags inside the lines matching the first expression. As a result, for example, you are counting occurrences of lines matching the first expression where there is a link on a line but where the keyword is not inside the href tag.

You need to make 2 passes (you should do 2 loops, putting the contents of the file into an array, so you don't have to read the file twice; this way you would have two foreach loops over the array).

Your regular expressions are also incorrect and should be
m/<title>.*?$mot[s]?.*?<\/title>/i
and
m/href=".*?$mot[s]?.*?"/i
...

Dal
0
[Dal] Posted messages 6122 Registration date   Status Contributor Last intervention   1 108
 
The date for the defense of your work having passed according to the .pdf file that is part of your thirty or so files, I don't think you will return to the forum. If you do come back, you are of course welcome, and I invite you to ask any questions about what you don't understand :-)

That said, this discussion may not be understood by other internet users coming across this topic.

So, for clarity, here’s what a corrected, commented program, removing unnecessary variables and syntax, and properly indented would look like:

#! /usr/bin/perl use strict; use warnings; my ($fich, $mot) = @ARGV; # reading the lines of the file into an array my $lec; open ($lec,"<",$fich) or die "error with $fich"; my @lines = <$lec>; close($lec); # points counter my $nb = 0; # counting the number of occurrences for this expression foreach (@lines) { if (/[ >]$mot[s]?[ \.\;\,\:<]/i) { $nb++; } } # each title or href tag containing at least once # the searched word gives extra points foreach (@lines) { if (/<title>.*?$mot[s]?.*?<\/title>/i) { $nb = $nb + 3; } if (/href=".*?$mot[s]?.*?"/i) { $nb = $nb + 2; } } # result print ""$fich $nb\n";

at execution:
$ ./exo2.pl page22.html cairo
page22.html 201
$
0