CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
Trying to Create Biology Example Program

 



noobiedoobie
New User

Jul 20, 2012, 8:51 AM

Post #1 of 2 (7736 views)
Trying to Create Biology Example Program Can't Post

Hi,

I am trying to create a simple example program for biological classification, I think a hash variable should work for this but I am having trouble.

It would be a basic sorting program that would provide students an example of how Perl is a language used in biology. I want to have them put in simple sentences based on facts given like:

A cat is a mammal.
A dog is a mammal.
A bluejay is a bird.
A snake is a reptile.
A lizard is a reptile.
A frog is an amphibian.
An owl is a bird.
A carp is a fish.

And then they could put in a question:

What is a carp?
>A carp is a fish.
What is a frog?
>A frog is an amphibian.

And more importantly (where I am having problems with my hash):

Who is a mammal?
>A cat is a mammal.
>A dog is a mammal.

Who is a reptile?
>A snake is a reptile.
>A lizard is a reptile.

Who is a bird?
>An owl is a bird.
>A bluejay is a bird.

I thought this would be an easy program to code and use as an example of Perl. Being a new to programming I have having trouble on this simple task.


BillKSmith
Veteran

Aug 15, 2012, 9:37 AM

Post #2 of 2 (6480 views)
Re: [noobiedoobie] Trying to Create Biology Example Program [In reply to] Can't Post

Who? amd What? questions need diferent hashes. The following example should get you started.


Code
  

use strict;
use warnings;
my %classification = get_rules();
my %who_is;
foreach my $animal (keys %classification) {
push @{$who_is{$classification{$animal}}}, $animal;
}
while ( (my $question = prompt('Enter question ("exit" to quit): ')) ne 'exit') {

if ($question =~ /^What/){
my $test = join '|', keys %classification;
my ($animal) = $question =~ /(:?$test)/;
if (!defined $animal) {
print "Your animal is not classified.\n";
next;
}
print "$animal is $classification{$animal}.\n";
}
elsif ($question =~ /^Who/) {
my $test = join '|', keys %who_is;
my ($catagory) = $question =~ /(:?$test)/;
if (!defined $catagory) {
print "Your catagory is not known.\n";
next;

}
foreach my $animal (@{$who_is{$catagory}}) {
print "$animal is $classification{$animal}.\n";
}
}

else {
print 'Question must start with "Who" or "What".', "\n";
}
}


sub prompt {
print $_[0];
my $response = <>;
chomp $response;
return $response;
}

sub get_rules {
my %classification;
while (my $rule = <DATA>){
my ($animal, $catagory) = $rule =~ /\AAn?\s(\w+)\sis\san?\s(\w+)\.\s*\z/;
$classification{$animal} = $catagory;
}
return %classification;
}
__DATA__
A cat is a mammal.
A dog is a mammal.
A bluejay is a bird.
A snake is a reptile.
A lizard is a reptile.
A frog is an amphibian.
An owl is a bird.
A carp is a fish.

Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives