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

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
All right, I'll jump in. I'm not exactly a newbie, but I'm also not an 'expert' (fill in your own opinion on what an 'expert' is).

So without further ado, this is what I ended up with:
#!/usr/bin/perl -w %couples = ( Fred=>"Wilma", Barney=>"Betty", George=>"Jane", Homer=>"Marge", Peter=>"Lois" ); print "$_ is married to $couples{$_}\n" for (sort keys %couples);

Looking at just the important line, I started with:
foreach $key (sort keys %couples) {print "$key is married to $couples{ +$key}\n"};
Hmm, exactly 80 characters. But wait, I can use the default variable ('$_') rather than creating my own, so I'm now down to 71 characters.
foreach (sort keys %couples) {print "$_ is married to $couples{$_}\n"} +;
Ahh, there's more. 'foreach' can be written as 'for' taking me done to 67 characters:
for (sort keys %couples) {print "$_ is married to $couples{$_}\n"};
Not quite - I can get rid of those pesky brackets by reversing the 'for' and 'print' statements, reducing it down to 65 characters:
print "$_ is married to $couples{$_}\n" for (sort keys %couples);

And that's as far as I've gotten. I've noticed that my solution got more 'perlish' (albeit not much) as I went along, so I was hoping that some of our more experienced monks would contribute as well - and notes as to how you got there are most welcome. Most Perl golf I see is from a realm I'm not familiar with, so a map to that wonderful place would be appreciated greatly.


P.S. Viewng my post in VIM with syntax checking on leads to some strange colored text...

In reply to Re: perl puzzle - cartoon couple registry (beginner, semi-golf) by BlueBlazerRegular
in thread perl puzzle - cartoon couple registry (beginner, semi-golf) by boo_radley

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?

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2023-05-28 09:02 GMT
Find Nodes?
    Voting Booth?

    No recent polls found