alphanumeric counter - howto?

D

Dr.Ruud

John W. Krahn:
$ perl -MPOSIX -le'print strtoul "ZZ", 36'
12950

Nice. But it acts weirdly on bad input:

$ perl -MPOSIX -le 'print strtoul "ZZ _", 36'
129514

$ perl -MPOSIX -le 'print strtoul "ZZ_________", 36'
12959
 
J

John W. Krahn

Dr.Ruud said:
John W. Krahn:


Nice. But it acts weirdly on bad input:

$ perl -MPOSIX -le 'print strtoul "ZZ _", 36'
129514

$ perl -MPOSIX -le 'print strtoul "ZZ_________", 36'
12959

As the documentation explains, strtol and strtoul return two values.

$ perl -MPOSIX -le 'print for strtoul "ZZ _", 36'
1295
14


John
 
D

Dr.Ruud

John W. Krahn:
Dr.Ruud:

As the documentation explains, strtol and strtoul return two values.

$ perl -MPOSIX -le 'print for strtoul "ZZ _", 36'
1295
14

Aargh, I totally missed the order of magnitude from base36('ZZ') to
base10(12950).
For the next time: 36x36 is about 1000, not 10000.


It's a pity though that (yes, as the documentation says) it doesn't work
AIWLI beyond base 36:

$ perl -MPOSIX -le 'print scalar strtoul("ZZ", 62)'
2205

$ perl -MPOSIX -le 'print scalar strtoul("ZZ", 63)'
2240

$ perl -MPOSIX -le 'print scalar strtoul("zz", 62)'
2205

$ perl -MPOSIX -le 'print scalar strtoul("zz", 63)'
2240
 
B

Brad Baxter

Dr.Ruud said:
John W. Krahn:


Nice. But it acts weirdly on bad input:

$ perl -MPOSIX -le 'print strtoul "ZZ _", 36'
129514

$ perl -MPOSIX -le 'print strtoul "ZZ_________", 36'
12959

Not weird according to the docs. The second value
returned is the number of unparsed characters.

perl -MPOSIX -le'print join "\n", strtoul "ZZ", 36'
1295
0

FWIW, strtol ends at base 36. My interest in this
thread is that for some time I've used base 62 in a
bitvector database index compression/storage
scheme. It started as an experiment, but ended up
working rather nicely. I haven't hit the 2 billion record
mark, so overflow isn't yet an issue I've worried about.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top