Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I get a hash to print all of it's contents to a file?? 1

Status
Not open for further replies.

pedros007

Programmer
Nov 14, 2004
8
0
0
GB
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
 
Code:
$Links{values}= $_ ;

Looks like you're overwriting the value with each value of the array @contents.
Code:
#! /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>;
$loop=0;
foreach(@content){
  if (m!^<a\s.*?href.*?\/a>$!) {
    $Links{$loop++}= $_ ;
  }
}
if ($UserChoice==1){
  open (NEWFH, ">HTML_Links.txt") || die "Can't create file: $!";
  print NEWFH  values %Links;
  close (NEWFH);    
  exit;
} elsif ($UserChoice==2){
  $num_keys=keys %Links;
  for ($loop=1; $loop<= $num_keys; $loop++) {
    print $Links{$loop};
  }
} else {
  print "Please enter choose either option 1 or 2 \n";
}

This isn't tested, but its probably closer to what you want

--Paul

Nancy Griffith - songstress extraordinaire,
and composer of the snipers anthem "From a distance ...
 
Where's the strict? *shifty eyes* Bad Paul!

:)

One thing I haven't seen before is "values"

Code:
print NEWFH  values %Links;

I deal with hashes on a daily basis and when I need them to spit output to a file, I always use

Code:
foreach my $key (%hash)
{
    print "$hash{$_}\n";
}

But I guess if your code does the same thing, ++ for you for making the code 3 lines shorter than mine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top