We don't bite newbies here... much PerlMonks

### Re: Alpha base-26 to base-10...

by BrowserUk (Pope)
 on Jun 30, 2003 at 23:32 UTC ( #270362=note: print w/replies, xml ) Need Help??

in reply to Alpha base-26 to base-10...

Feel free to not use the name:)

```sub b262b10{ my (\$n,\$x)=(0,'A'); ++\$n until \$x++ eq \$_[0]; ++\$n }

print \$_, ':', b262b10( \$_ ), \$/ for qw[A Z AA AZ BA ZZ AAA ZBA]
A : 1
Z : 26
AA : 27
AZ : 52
BA : 53
ZZ : 702
AAA : 703
ZBA : 17629

Update: Lest anyone not realise it, this okay as a golf solution, but it is not a serious contender for anything real as it is horribly inefficient.

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

Replies are listed 'Best First'.
Re: Re: Alpha base-26 to base-10... (golf)
by sauoq (Abbot) on Jul 01, 2003 at 03:48 UTC

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.";
```
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?

Create A New User
Node Status?
node history
Node Type: note [id://270362]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2017-12-12 03:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What programming language do you hate the most?

Results (324 votes). Check out past polls.

Notices?