NAME
Number::Nary - encode and decode numbers as n-ary strings
VERSION
version 0.04
$Id: /my/rjbs/code/nary/trunk/lib/Number/Nary.pm 16007 2005-11-07T13:08:21.179599Z rjbs $
SYNOPSIS
This module lets you convert numbers into strings that encode the number using the digit set of your choice.
my ($encode_sub, $decode_sub) = n_codec('01234'); # base five
my $number = $encode_sub('8675309'); # jenny's number for one-handed folk
FUNCTIONS
n_codec
my ($enc, $dec) = n_codec("123456890"); # we hate seven
my ($enc, $dec) = n_codec([ qw( foo bar baz ) ]); # metasyntacticary
This routine returns a reference to a subroutine which will encode numbers into the given set of digits and a reference which will do the reverse operation.
$digits
may be a string, which will be split into individual characters, or an array reference of the digits to be used.
This routine will croak if the digit string contains repeated digits or digits of non-uniform length.
The encode sub will croak if it is given input other than a non-negative integer.
The decode sub will croak if given a string that contains characters not in the digit string, or if the given string's length is not a multiple of the digit length.
n_encode($value, $digits)
This encodes the given value into a string using the given digit string. It is written in terms of n_codec
, above, so it's not efficient at all for multiple uses in one process.
This routine is not exported by default.
n_decode($value, $digits)
This is the decoding equivalent to n_encode
, above.
This routine is not exported by default.
AUTHOR
Ricardo Signes, <rjbs at cpan.org>
BUGS
Please report any bugs or feature requests to bug-number-nary@rt.cpan.org
, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Number-Nary. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
SECRET ORIGINS
I originally used this system to produce unique worksheet names in Excel. I had a large report generating system that used Win32::OLE, and to keep track of what was where I'd Storable-digest the options used to produce each worksheet and then n-ary encode them into the set of characters that were valid in worksheet names. Working out that set of characters was by far the hardest part.
ACKNOWLEDGEMENTS
Thanks, Jesse Vincent. When I remarked, on IRC, that this would be trivial to do, he said, "Great. Would you mind doing it?" (Well, more or less.) It was a fun little distraction.
COPYRIGHT & LICENSE
Copyright 2005 Ricardo Signes, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.