shels
asked on
Reading from a file
Hi guys I'm hoping somebody can help me with what is probably quite an easy task for the experts out there!
Could somebody please tell what I need to add or change in the following code (I know the first line is gone straight away) so that instead of reading input from the keyboard the program will read from a file in my directory and then print the results to the screen.
Nothing is working for me so far, I reckon I'm probably just making silly mistakes so I'd appreciate a bit of help - ta v. much!
#!/usr/bin/perl
print "Please enter the heading of your text to be categorized \n";
chomp ($string = <STDIN>);
#chop $string;
@aswordlist = qw(asthma inhale inhaler inhaled chest airway airways breath breathing breathe agonists exercise lung lungs nedocromil leukotriene);
@word{ @aswordlist } = (); # this is the %word hash
while ($string =~ /(\S+)/g) { # groups of non-whitespace characters
if (exists $word{$1}) { # $1 is that group of characters
$word{$1}++; # increment the times it was found
}
}
# variable that keeps a total of all the keywords found!!
$total_count=0;
for (@aswordlist) {
if ($word{$_}) { # if there was at least 1 match for the word
$total_count++;
}
}
# NOW, print the results..
print "$total_count asthma keyword(s) were found in the heading.\n";
if ($total_count > 0)
{
print "This text is probably related to asthma.\n";
}
elsif ($total_count < 1)
{
print "This text is probably not related to asthma.\n";
}
Could somebody please tell what I need to add or change in the following code (I know the first line is gone straight away) so that instead of reading input from the keyboard the program will read from a file in my directory and then print the results to the screen.
Nothing is working for me so far, I reckon I'm probably just making silly mistakes so I'd appreciate a bit of help - ta v. much!
#!/usr/bin/perl
print "Please enter the heading of your text to be categorized \n";
chomp ($string = <STDIN>);
#chop $string;
@aswordlist = qw(asthma inhale inhaler inhaled chest airway airways breath breathing breathe agonists exercise lung lungs nedocromil leukotriene);
@word{ @aswordlist } = (); # this is the %word hash
while ($string =~ /(\S+)/g) { # groups of non-whitespace characters
if (exists $word{$1}) { # $1 is that group of characters
$word{$1}++; # increment the times it was found
}
}
# variable that keeps a total of all the keywords found!!
$total_count=0;
for (@aswordlist) {
if ($word{$_}) { # if there was at least 1 match for the word
$total_count++;
}
}
# NOW, print the results..
print "$total_count asthma keyword(s) were found in the heading.\n";
if ($total_count > 0)
{
print "This text is probably related to asthma.\n";
}
elsif ($total_count < 1)
{
print "This text is probably not related to asthma.\n";
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you so much for that it's a great help! Thanks also to Ozo
it looks like it could work as is if invoked with
perl MyPerlProgram <FileInMyDirectory
or if you prefer, you could add a line like:
open STDIN,"<FileInMyDirectory"