Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Simple Greeter

by Aristotle (Chancellor)
on May 02, 2003 at 01:18 UTC ( [id://254866]=note: print w/replies, xml ) Need Help??


in reply to Simple Greeter

You can assign a list of results to a list of variables all at once. printf or sprintf are the tools of choice when you have to pad stuff for presentation on screen. Try to avoid superfluous temporary variables wherever possible.
#!/usr/bin/perl -w use strict; print "Please enter your name: "; chomp(my $name = <STDIN>); my @greeting = ("morning", "afternoon", "evening"); my @days = qw/ Sunday Monday Tuesday Wednesday Thursday Friday Saturda +y /; my @months = qw/ January February March April May June July August Sep +tember October November December /; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime; $year += 1900; my $greeting = $hour < 12 ? 0 : $hour < 17 ? 1 : 2; print <<"EOT", $hour, $min; Good $greeting[$greeting], $name. Today is $days[$day], $months[$mon] $mday, $year. The time is now %02d:%02d. EOT
(Untested, might have a typo or two.)

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re: Simple Greeter
by harley_frog (Novice) on May 02, 2003 at 16:42 UTC
    Thanks for the input (and code). I've only started studying Perl a week ago, and all this information is helpful, even though I may not fully understand it until later. I will most likely revisit this program several times, making changes along the way as my skills progress. But for an absolute beginner in Perl (or any programming for that matter), I'm surprised that I wrote something that actually works without having to rip out my hair. LOL

    Frog

    Update: I ran the code and noticed one typo. $days[$day] should read $days[$wday]. The time at the end didn't print out correctly, but this is well beyond my minisucle bit of Perl knowledge to decypher. I'm going to tuck this code away and look at it when I know more and can understand better. Like I said, this script was written mostly as a learning tool for me. :)

    Frog

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 03:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found