Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Re: Alpha base-26 to base-10... (golf)

by sauoq (Abbot)
on Jul 01, 2003 at 03:48 UTC ( #270391=note: print w/ replies, xml ) Need Help??


in reply to Re: Alpha base-26 to base-10...
in thread Alpha base-26 to base-10...

Did you say "golf"? :-)

1 2 123456789012345678901234567 sub b262b10{ my$n;$n++for"a"..lc$_[0];$n }

27

Update: Make that 26. I can save a stroke by reusing @_. :-)

sub b262b10{ $_[1]++for"a"..lc$_[0];pop }
-sauoq
"My two cents aren't worth a dime.";


Comment on Re: Re: Alpha base-26 to base-10... (golf)
Select or Download Code
Re: Re: Re: Alpha base-26 to base-10... (golf)
by bwana147 (Pilgrim) on Jul 01, 2003 at 10:46 UTC
    Golf?
    sub b262b10{ ()=a..lc pop }

    Explanation:

    In Golf, there's no need for strict or quotes. A bareword is a string when Perl can't find an identifier with that name. So here, a is just "a", only two strokes shorter.

    pop takes one argument off of @_, and since there's only one argument, it's just the same as shift. lc turns it to lower case.

    .. is the range operator in list context, but the flip-flop operator in scalar context. The construct ()= forces list context. The value of a list assignment is the number of elements of the right member, even though the left value (()) is empty.

    So this sub yields (in scalar context) the number of element between "a" and the string passed as argument. QED

    --bwana147

      what?

      Care to explain that?

        To get it, try this:

        regards,
        tomte


        Hlade's Law:

        If you have a difficult task, give it to a lazy person --
        they will find an easier way to do it.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2014-12-25 05:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (159 votes), past polls