Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: **HomeWork** Printing the found input from an array

by wrinkles (Monk)
on Apr 02, 2008 at 09:20 UTC ( #677919=note: print w/ replies, xml ) Need Help??


in reply to **HomeWork** Printing the found input from an array

I made a few changes, including substituting "unless" a couple of times, and changing the month hash to a list.

#!/usr/bin/perl #update: some problems with the month array in my code belo w (I was t +hinking the index starts at 1 not 0, newbie mistake. See discussion. use strict; use warnings; print "Excuse Generator\n\n\n"; my @myCities = ('Baltimore','Chicago','Los Angeles','New York','San Di +ego'); my $found=0; print "Please pick one of the following cities by entering any portion + of the beginning of the city name:(@myCities)\n"; chomp(my $input_city = <STDIN> ); if ( $input_city =~ m/^l/i ) { print "We no longer tour the city of Angels, please try again\ +n"; exit; } foreach my $city ( @myCities ) { if ($city =~ /^$input_city/i ) { $found = $city; print "Found $found\n"; next; } } unless ($found) { print "No City found!\n"; exit; } my @months = qw(January February March April May June July August Sept +ember October November December); print "Enter the month as an integer i.e. (1=January, 2=Febuary, etc.) +\n"; chomp(my $month = <STDIN> ); print "Invalid Month $month specified\n" unless (exists $months[$month +]); print "How many days [1-31]?\n"; chomp(my $num_days = <STDIN> ); #problems here, see discussion my $days = join( ", ", 1 .. $num_days ),; my @myArray = ( "\nHi Steve, \n\n", "I'm looking forward to the Perl class. \n", "I just want to let you know that I won't be in class on the f +ollowing days:\n\n", "\ $months[$month] $days\n\n", "I'm touring $found with my dance band, Liquid Blue!\n\n", "Thanks,\n", "Scott Stephens\n\n" ); print @myArray;


Comment on Re: **HomeWork** Printing the found input from an array
Download Code
Re^2: **HomeWork** Printing the found input from an array
by toolic (Chancellor) on Apr 02, 2008 at 13:12 UTC
    changing the month hash to a list
    This prints the incorrect month name because an array index starts at 0, not 1. This will fix it:
    chomp(my $month = <STDIN> ); $month--;
Re^2: **HomeWork** Printing the found input from an array
by ww (Bishop) on Apr 02, 2008 at 13:56 UTC
    toolic's point is well taken, but that "fix" is only partial. If, at runtime, one enters 0 for the month number, we see this:
    Enter the month as an integer i.e. (1=January, 2=Febuary, etc.) 0 How many days [1-31]? 31 Hi Steve, I'm looking forward to the Perl class. I just want to let you know that I won't be in class on the following +days: December 1, 2, 3, 4, 5, 6, 7, ....
    So one still needs to test another element of validity in the input:
    chomp(my $month = <STDIN> ); $month--; print "Invalid Month $month specified\n" unless (exists $months[$month +]); unless (exists $months[$month] && ( $month >=0 && $month <= 11 ) ){ print" Quitting. Insufficient user IQ or typing skills\n"; exit; }

    And, similarly with the day_of_month entry.

    Further, looking back to OP's test for cities, telling the user that only a restricted list is acceptable is fine, but if the user ignores that, and enters, say, ( Livonia || Las Vegas || Los Gatos ),that user still gets an answer (clear enough here, but perhaps not in a more complicated scenario) which refers only to Los Angeles, and thus may fail to help the (inattentive) luser understand what's wrong.

    Undoubtedly outside the scope of this particular bit of homework, but worth thinking about: Checking for invalid input and providing feedback are highly desireable and designing checks with broad coverage may require a lot of thought about how the user could "screw things up."

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://677919]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (12)
As of 2014-08-01 04:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (256 votes), past polls