Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hello all. Recently a friend asked me to write a script to do the following:

1. user inputs start month/day, end month/day;
2. randomly choose breakfast, lunch, dinner every day during that span.
3. save to file

I won't bore you with why he wanted this, but here's what I came up with:

#!/usr/bin/perl use warnings; use strict; my %months = ( 1 => "Jan", 2 => "Feb", 3 => "Mar", 4 => "Apr", 5 => "M +ay", 6 => "June", 7 => "July", 8 => "Aug", 9 => "Sept", 10 => "Oct", +11 => "Nov", 12 => "Dec" ); my @breakfast = qw/bagel cereal toast yogurt/; my @lunch = qw/sandwich milkshake pb&j hoagie/; my @dinner = qw/pasta rice chicken steak/; my $file = ''; my ($start_day, $start_mon, $end_day, $end_mon); my ($total, $len, $curr_mon, $curr_day); my $three_one = join ('|', qw/1 3 5 7 8 10 12/); #months with 31 days my $three_zero = join ('|', qw/4 6 9 11/); #months with 30 days print "Enter start month (ie., for January, enter 1): "; chomp($start_mon = <STDIN>); print "Enter start day (ie., 20): "; chomp($start_day = <STDIN>); print "Enter end month (ie., for March, enter a 3): "; chomp($end_mon = <STDIN>); print "Enter end day (ie., 21): "; chomp($end_day = <STDIN>); print "Save to file [hit enter for default 'plan']: "; chomp($file = <STDIN>); if($file eq '') { $file = "plan"; } open(FH, ">$file") or die "open: $!\n"; $curr_mon = $start_mon; $curr_day = $start_day; $total = get_days(); # get # of days for the current month while (1) { last if($curr_mon eq $end_mon && $curr_day == $end_day + 1); print FH "$months{$curr_mon} $curr_day\n"; $len = length($months{$curr_mon}) + length($curr_day) + 1; print FH "-" x $len, "\n"; print FH "Breakfast: ", $breakfast[ rand @breakfast], "\n"; print FH "Lunch : ", $lunch[ rand @lunch], "\n"; print FH "Dinner : ", $dinner[ rand @dinner], "\n"; print FH "-" x $len, "\n"; $curr_day++; if($curr_day > $total) { $curr_mon++; $curr_day = 1; $total = get_days(); } } print "Written to file $file.\n"; close(FH); sub get_days { if($curr_mon =~ /$three_one/) { return 31; } elsif($curr_mon =~ /$three_zero/) { return 30; } elsif($curr_mon == 2) { return 29; } else { die "Bad month entered!\n"; } }

It works fine - it just seems long and not very neat. I'm still a beginner and was wondering how more experienced monks might have written it.


In reply to Critique by phenom

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found