Hi,
I am very new to Perl, 3 days old in fact. I hav written a simple program which extracts web links from a file which the user specifies and then either prints the results on the screen or saves it to a file. However when it saves the results to the file it only saves the furst entry and I do not know why. Please Help!
Thanks
Pete
Here is my code below, I hope this is ok!
#! /usr/bin/perl
#User enters in the name of the file, of which they would like to be checked for HTML Links
print "Please enter the file name including the extension of the file\n";
$file = <STDIN>;
chomp ($file);
open (file, "$file") or die "Can't find the file";
@content=<file>;
close(file);
print "Please choose either option 1 or 2 \n";
print "1 - Save the extracted links to file \n";
print "2 - Print the HTML links on Screen \n";
$UserChoice = <STDIN>;
foreach(@content){
if (m!^<a\s.*?href.*?\/a>$!) {
$Links{values}= $_ ;
if ($UserChoice==1) {
open (NEWFH, ">HTML_Links.txt") || die "Can't create file: $!";
print NEWFH values %Links;
close (NEWFH);
exit
}
elsif ($UserChoice==2) {
print $Links{values};
}
else {
print "Please enter choose either option 1 or 2 \n";
}
}
}
Cheers
Pete
I am very new to Perl, 3 days old in fact. I hav written a simple program which extracts web links from a file which the user specifies and then either prints the results on the screen or saves it to a file. However when it saves the results to the file it only saves the furst entry and I do not know why. Please Help!
Thanks
Pete
Here is my code below, I hope this is ok!
#! /usr/bin/perl
#User enters in the name of the file, of which they would like to be checked for HTML Links
print "Please enter the file name including the extension of the file\n";
$file = <STDIN>;
chomp ($file);
open (file, "$file") or die "Can't find the file";
@content=<file>;
close(file);
print "Please choose either option 1 or 2 \n";
print "1 - Save the extracted links to file \n";
print "2 - Print the HTML links on Screen \n";
$UserChoice = <STDIN>;
foreach(@content){
if (m!^<a\s.*?href.*?\/a>$!) {
$Links{values}= $_ ;
if ($UserChoice==1) {
open (NEWFH, ">HTML_Links.txt") || die "Can't create file: $!";
print NEWFH values %Links;
close (NEWFH);
exit
}
elsif ($UserChoice==2) {
print $Links{values};
}
else {
print "Please enter choose either option 1 or 2 \n";
}
}
}
Cheers
Pete