in reply to Re: Golf: string complement
in thread Golf: string complement

How about we lose another 7 characters :)
sub invert { #23456789012345678901234567890 grep/[^\Q@_\E]/,map{chr}1..255 }

cLive ;-)

updare - to summarize response to BrowserUK's comment - calling the sub in scalar context will automatically join the list into a scalar. Is that cheating? maybe :) - never mind, I was running the wrong test script lol :)

Replies are listed 'Best First'.
Re: Re:^2 Golf: string complement
by BrowserUk (Patriarch) on May 15, 2004 at 23:38 UTC

    Problem: That will return a list of characters, not a string.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
      But, when you print, it forces scalar context. The output of the print statement is the same. Same if you assigned result of sub to a scalar explicitly:
      my $result = invert($string);
      is the same with or without the join.

      .02

      cLive ;-) - see above

        Not quite. A list grep in a scalar context return the size of the list.

        sub invert { #23456789012345678901234567890 grep/[^\Q@_\E]/,map{chr}1..255 }; $inverted = invert( 'Just Another Perl Hacker!' ); print $inverted; 237

        237 + 18 (unique characters in the string) = 255.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        Huh? grep() in scalar context returns a number, not a join()ed list of elements that passed through. And print() doesn't "force scalar context" -- in fact, it invokes list context, which is the only reason why print invert($string) works.
        _____________________________________________________
        Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
        s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;