Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: perl puzzle - cartoon couple registry (beginner, semi-golf)

by mrbbking (Hermit)
on Jun 11, 2002 at 23:57 UTC ( #173697=note: print w/replies, xml ) Need Help??

in reply to perl puzzle - cartoon couple registry (beginner, semi-golf)

My solution ended up nearly identical to BlueBlazerRegular's, but I dropped a bit of excess punctuation and whitespace, to come in with 21 chars to spare:
# 1 2 3 4 5 6 #23456789012345678901234567890123456789012345678901234567890 print"$_ is married to $couples{$_}\n"for sort keys%couples
Bonus (for me, anyhow): I played with map a bit, and found that it was actually longer that way.
# 1 2 3 4 5 6 #2345678901234567890123456789012345678901234567890123456789012 print map{"$_ is married to $couples{$_}\n"}sort keys%couples
Update: Whoops. I can get away with '\n' since it's not an actual 'newline', but may casual disregard of the "can't use a backslash" rule was just silly. I'm out!

Replies are listed 'Best First'.
Re: (Revelation) Re: A Bit Of Help
by Revelation (Deacon) on Jun 12, 2002 at 18:00 UTC
    Building off mrbbking, why don't we try and get rid of \n. After thinking and messing around bit, I ended up using $*: The multiline matching variable. I'd love to know *why* this works, and if it's compatible with *nix, as I used activestate.

    Update: Tested on a FreeBSD shell, and it *does* work, as long as we run it with perl -l...

    Please don't run this with warnings, as it won't work because $* is a deprecated special variable.

    Here Goes: Still 59 Letters
    #! perl -l %couples = ( Fred=>"Wilma", Barney=>"Betty", George=>"Jane", Homer=>"Marge", Peter=>"Lois" ); print"$_ is married to $couples{$_}$*"for sort keys%couples
    Another idea that may or may not have merit is to set %couples to a shorter word, and call that in the print. Something that would (logically) be *like*
    print"$_ is married to $c{$_}$*"for sort keys(%c=%couples)
    That code doesn't work, though, and would save you just one character.

    Update 2: I found it strange that I was getting newlines, instead of 0 or 1 from $*, so I investigated. From perldoc perlrun: -l enables automatic line-ending processing. If octnum is omitted, sets $\ to the current value of $/. That means that there's no reason to use $*, as -l will give us $/, anyways.

    You will then realize that the previous answers would be correct, if we got rid of \n, or in my case $* (-l chops the last letter off in addition, which meant that my $* which should have been evaluated as 0 or 1, was being chopped off and replaced by a newline ($/). Strage, but we don't even need to print newlines, with perl -l :)

    That means: print"$_ is married to $couples{$_}"for sort keys%couples can replace the respective line in the previous solution(s)

    Gyan Kapur
      Damn keys
      print"$_ is married to $a{$_}$*"for sort keys%{*a=*couples}
      is the same whereas
      print"$_ is married to $a{$_}$*"for sort keys*a=*couples
      is no go and
      print"$_ is married to $a{$_}$*"for sort keys%a=%couples
      is just not right...

      perl -pew "s/\b;([mnst])/'$1/g"

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2023-03-26 08:48 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (63 votes). Check out past polls.