Solved

Reading from a file

Posted on 2000-05-14
3
140 Views
Last Modified: 2010-03-05
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
Comment
Question by:shels
3 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
 
LVL 12

Accepted Solution

by:
geotiger earned 50 total points
Comment Utility
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
 

Author Comment

by:shels
Comment Utility
Thank you so much for that it's a great help! Thanks also to Ozo
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now