• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 157
  • Last Modified:

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";
   }
0
shels
Asked:
shels
1 Solution
 
ozoCommented:
Why do you need to add or change anything?
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" or die "Can't open FileInMyDirectory because $!";
0
 
geotigerCommented:
I modified your code so that:
1. if the first input is a file, it will open the file and assign the content to an array
2. it will accept multiple inputs.
3. check all the inputs
4. print the result.


$ more tst07inp.pl
#!/usr/local/bin/perl

$msg = "\nPlease enter the heading of your text to be categorized \n";
$msg .= "or a file containing a list of answers:\n";

print $msg; chomp ($string = <STDIN>);
if (-f $string) {  # we have got a file!
   open FILE, "<$string" or die "Could not open file - $string: $!\n";
   @a = <FILE>;
   close FILE;
} else {
   $a[0] = $string;
   $i= 0;
   $moreinput = 1;
   while ($moreinput) {
       print "More input(Y/N)? ";
       chomp ($string = <STDIN>);
       if (uc($string) eq "Y") {
           print $msg; chomp ($string = <STDIN>);
           ++$i;
           $a[$i] = $string;
       } else {
          $moreinput = 0;
       }
   }
}

print join ",", @a;
print "\n";

  $aswordlist  = "asthma|inhale|inhaler|inhaled|chest|airway|airways";
  $aswordlist .= "|breath|breathing|breathe|agonists|exercise|lung";
  $aswordlist .= "|lungs|nedocromil|leukotriene";
  %word = ();  # this is the %word hash

$total_count=0;
for $i (0..$#a) {     # loop through all the answers
    $string = $a[$i];
    if ($string =~ /($aswordlist)/) {  # $1 is that group of characters
      $word{$1}++;               # increment the times it was found
      ++$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";
   }


$ ./tst07inp.pl

Please enter the heading of your text to be categorized
or a file containing a list of answers:
asthma
More input(Y/N)? y

Please enter the heading of your text to be categorized
or a file containing a list of answers:
chest
More input(Y/N)? y

Please enter the heading of your text to be categorized
or a file containing a list of answers:
lung
More input(Y/N)? y

Please enter the heading of your text to be categorized
or a file containing a list of answers:
sadfdsaf
More input(Y/N)? n
asthma,chest,lung,sadfdsaf
3 asthma keyword(s) were found in the heading.
This text is probably related to asthma.
0
 
shelsAuthor Commented:
Thank you so much for that it's a great help! Thanks also to Ozo
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now