Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Reading from a file

Posted on 2000-05-14
3
147 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
ID: 2808892
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
ID: 2809315
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
ID: 2810603
Thank you so much for that it's a great help! Thanks also to Ozo
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…

838 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