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: Perl Programming Help: Beginner:
Coding Issues

 



dannyb_16
Novice

Sep 22, 2013, 1:18 PM

Post #1 of 6 (605 views)
Coding Issues Can't Post

I am a new user to Perl so please bare with me. I have created a small script which was designed to test a "hash of hashes". What the script does basically is asks the user what film/tv program they want the rating for, and then the if statement will display the rating for that program. The code is below:

#!/usr/bin/perl


%hash = (

'Films' => {

'Harry Potter' => "This film Has been rated 8.5\n",
'Titanic' => "This film has been rated 9.0\n",
'Cloverfield' => "This film has been rated 6.5\n",
'Jarhead' => "This film has been rated 7.0\n",

},

'TV' => {

'Supernatural' => "This series has been rated 8.0\n",
'Family Guy' => "This series has been rated 9.0\n",
'South Park' => "This series has been rated 9.5\n",
'Will & Grace' => "This series has been rated 7.5\n",

},

);

print "What film or series would you like to know the rating for?\n";


chomp ($answer = <STDIN>);

if ($answer == "Supernatural") {
print $hash{'TV'}{'Supernatural'}


} elsif ($answer == "Titanic") {
print $hash{'Films'}{'Titanic'}



};


...........................................

So basically, if the user enters "Supernatural" for example, the rating for supernatural should be called from the list of hashes which it is. However, if the user types in "Titanic" as the other option, they do not get the rating of 9.0 for Titanic, they receive the 8.0 for the first one. In fact, anything the user types in, they will receive the output 8.0.

Im not sure if i have made an error with my if statement of what. This was just a test however, what my overall goal is, is to have a statement which would present any of the ratings in the array which im assuming there is an easier way of doing rather than just writing 'elsif' for ever value in the hash.

Can someone tell me if there is an error in my code please. Thanks


BillKSmith
Veteran

Sep 22, 2013, 5:45 PM

Post #2 of 6 (599 views)
Re: [dannyb_16] Coding Issues [In reply to] Can't Post

You must compare strings with eq not ==

== is used for comparing the value of numbers
Good Luck,
Bill


2teez
Novice

Sep 23, 2013, 9:38 AM

Post #3 of 6 (588 views)
Re: [dannyb_16] Coding Issues [In reply to] Can't Post

Hi dannyb_16,
And what if you want to print for other shows in each of the categories? Will you continue to use if/else for each of the cases?
Or if you have to get for more than one case, will you be running script again and again?

The following is an improvement on what you did.
And of course, you can write Perl script as you want, but believe me you will need to include

Code
use warnings; 
use strict;

except you are using a higher version say from 5.12.0 upward.

Code
#!/usr/bin/perl  
use warnings;
use strict;

my %hash = (

'Films' => {

'Harry Potter' => "This film Has been rated 8.5\n",
'Titanic' => "This film has been rated 9.0\n",
'Cloverfield' => "This film has been rated 6.5\n",
'Jarhead' => "This film has been rated 7.0\n",

},

'TV' => {

'Supernatural' => "This series has been rated 8.0\n",
'Family Guy' => "This series has been rated 9.0\n",
'South Park' => "This series has been rated 9.5\n",
'Will & Grace' => "This series has been rated 7.5\n",

},

);

while (1) {
print "What film or series would you like to know the rating for?\n";

chomp( my $answer = <STDIN> );

last if $answer =~ /\bquit\b/;

for my $key ( keys %hash ) {
for ( keys %{ $hash{$key} } ) {
print $hash{$key}{$answer} if $answer eq $_;
}
}
}



dannyb_16
Novice

Sep 23, 2013, 11:06 AM

Post #4 of 6 (580 views)
Re: [2teez] Coding Issues [In reply to] Can't Post

Thank you 2teez.

I have been trying to achieve the script you posted for a number of hours but was unsuccessful. Thank you for your help.


BillKSmith
Veteran

Sep 23, 2013, 2:22 PM

Post #5 of 6 (572 views)
Re: [dannyb_16] Coding Issues [In reply to] Can't Post

Even better, use a prompt module.

Code
#!/usr/bin/perl 
use warnings;
use strict;
use IO::Prompt::Hooked;
my %hash = (
Films => {
'Harry Potter' => 'This film Has been rated 8.5',
'Titanic' => 'This film has been rated 9.0',
'Cloverfield' => 'This film has been rated 6.5',
'Jarhead' => 'This film has been rated 7.0',
},
TV => {
'Supernatural' => 'This series has been rated 8.0',
'Family Guy' => 'This series has been rated 9.0',
'South Park' => 'This series has been rated 9.5',
'Will & Grace' => 'This series has been rated 7.5',
},
);
my %options = (
message => 'What film or series would you like to know the rating for?',
error => "I am not familiar with that work.\n",
tries => 5,
default => 'quit',
validate => sub {
my $work = shift;
return 1 if $work eq 'quit';
return (exists $hash{TV}{$work} or exists $hash{Films}{$work});
},
);
while (my $answer = prompt(%options)){
last if $answer =~ /\bquit\b/;
if (exists $hash{Films}{$answer}) {
print $hash{Films}{$answer},"\n";
}
else {
print $hash{TV}{$answer}, "\n";
}
}

Good Luck,
Bill


Laurent_R
Veteran / Moderator

Sep 25, 2013, 11:53 AM

Post #6 of 6 (546 views)
Re: [dannyb_16] Coding Issues [In reply to] Can't Post

What is not working properly? I agree with 2teez that you really don't want to use something as clunky as the series of if/else you were using. There may be something wrong in 2teez's code (I haven't tried it), but it definitely goes in the right direction.

I appreciate that you want to work on hashes of hashes, but, in this specific case, I would use a simple hash, with the name of the movie or TV series as the key of the hash. Then all you need to do is to chomp the user answer and print directly the hash value corresponding to this answer if it exists. Something like this:


Code
my $answer = <>; 
chomp $answer;
if (exists $hash{$answer}) {
print $hash{$answer} ;
} else {
print "try again \n";
}


 
 


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

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