Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Another "Silly use for Perl" entry.

Anonymous Monk asked for a method for incrementing mixed letters and numbers recently, which particular need is satisfied with Math::Base36. Can we do better? I guess, yes.

use 5.10.0; use Math::Base; my $begin = Math::Base->new(36, 1009, 1); # base, number, is_encoded my $end = Math::Base->new(36, 1020, 1); my $c = Math::Base->new(36, 42); say $c->encode($_) for $begin .. $end; # 1009 # 100A # 100B # 100C # ... # 101X # 101Y # 101Z # 1020 # also (with updated code below) # my $x = Math::Base->new(36, 46664); # 1008 in base36 # say ++$x for 0..63; # output same as above # Arithmetics with different encodings: $p = Math::Base->new(8,777,1); # decimal 511 $z = Math::Base->new(36, 35); # 'Z' as base36 say $z * $p; # 42735 (octal) say $p * $z; # 'DST' (base36) # Changing the string representation: $s = Math::Base->new(16,18); say $s; # 12 $s->rebase(18); say $s; # 10 $s += 3; # 13 $s->rebase(2); say $s; # 10101 # Get decimal value: $xyz = Math::Base->new(64, 'XYZabc', 1); say $xyz->num; # 36013230438

Far from complete, but fun enough yet. For me, that is... ;-)

Update: Below is an updated version which handles negative numbers, implements missing operators and lets you define your own charset for baseX conversion, e.g. to calculate base3 with qw(a b c). Also, a method integer() is added which emulates use integer globally for all calculations, and some utility methods/functions.

Update: fixed some bugs

I'll eventually make it into a CPAN package proper.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

In reply to Math::Base - arithmetics with baseX integers (updated) by shmem

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-04-26 07:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found