Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Yow! That code is almost obfuscated! At the very least, if you are teaching people perl, please always check the return value of 'open'! Here is my rewrite, trying to maintain some of the spirit of the original:
#!perl use strict; my(@santa); my($total,$x,$y,$found,$old); my(%gift); my($name, $name2, $email, $email2); while(<>) { chomp; m/::/ && push(@santa, $_); } my $mail = "/usr/lib/sendmail -t -oi -odq"; my $mailfrom = "kris.kringle\@north.pole"; my $subject = "Your Kris Kringle Recipient"; ## Give everyone a present from a random person: $total=0; srand; for $y (@santa) { $found=0; $total++; while (!$found) { $x = $santa[rand @santa]; $y eq $x && next; ## No presents to self! ## What if the only person left is yourself? This solves that: if ($total == @santa && !$gift{$y}) { ## Switch with another! $old = $gift{$x}; $gift{$x}=$y; $gift{$y}=$old; last; } $gift{$x} && next; ## No more than one present per person $gift{$x}=$y; $found++; } } ## Now we send out the email: for $y (@santa) { ($name, $email) = split(/::/, $y); ($name2, $email2) = split(/::/, $gift{$y}); open(MAIL, "|$mail $email") || die "Could not open $mail: $!\n"; print MAIL <<"NORTHPOLE"; From: $mailfrom To: "$name" <$email> Subject: $subject $subject is: $name2 ($email2) K.K. Please do NOT reply! 'I' am a program: NORTHPOLE open(SELF, $0) || die "Could not open $0: $!\n"; while(<SELF>) { print MAIL; } close(SELF) || die "Could not close $0: $!\n"; close(MAIL) || die "Could not close $0: $!\n"; } exit;
The little section that begins "if ($total == @santa...)" is there to prevent the following condition. Say we have three people, A, B. and C. A randomly gets assigned to B, then B gets assigned to A. At this point, the only person C can give a present to who has not gotten one is herself, which is not allowed! Hence, the little snippet above, which neatly solves that. Enjoy!

In reply to RE: Kris Kringle Script by turnstep
in thread Kris Kringle Script by Marburg

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 lurking in the Monastery: (7)
As of 2024-04-25 15:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found