Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   autoincrement hex numbers (http://www.velocityreviews.com/forums/t897513-autoincrement-hex-numbers.html)

 cartercc@gmail.com 04-17-2006 03:34 PM

autoincrement hex numbers

Can Perl autoincrement hex numbers?

Our problem is that we have a field only four characters wide, and we
anticipate that we may have more than 10,000 records to uniquely
identify in this field. We need to autoincrement this field.

Many, many TIA, CC.

 Nico Coetzee 04-17-2006 04:49 PM

Re: autoincrement hex numbers

cartercc@gmail.com wrote:
> Can Perl autoincrement hex numbers?
>
> Our problem is that we have a field only four characters wide, and we
> anticipate that we may have more than 10,000 records to uniquely
> identify in this field. We need to autoincrement this field.
>
> Many, many TIA, CC.
>

use Math::BaseCnv;
my \$hex = \$ARGV[0];
my \$num = cnv( \$hex, 16, 10 );
\$num++;
\$hex = cnv( \$num, 10, 16 );

 xhoster@gmail.com 04-17-2006 04:50 PM

Re: autoincrement hex numbers

cartercc@gmail.com wrote:
> Can Perl autoincrement hex numbers?

Not inherently. Strictly speaking, there are no hex numbers, only hex
representations of numbers. You could probably make a module (or find
one on CPAN) that will alter make stringification automatically produce
hex representations, and numification automatically use hex
representations.

> Our problem is that we have a field only four characters wide, and we
> anticipate that we may have more than 10,000 records to uniquely
> identify in this field. We need to autoincrement this field.

Do you really need to autoincrement it? What is wrong with using some
something other than autoincrement, like, say hex_increment(\$thing)?

sub hex_increment {
\$_[0] = sprintf "%x", 1+hex\$_[0];
};

Or how about using base 26 rather than base 16? Then you could just use
the stringy autoincrement.

Xho

--
Usenet Newsgroup Service \$9.95/Month 30GB

 Uri Guttman 04-17-2006 09:59 PM

Re: autoincrement hex numbers

>>>>> "NC" == Nico Coetzee <blabla@yourguess.com> writes:

NC> cartercc@gmail.com wrote:
>> Can Perl autoincrement hex numbers?
>> Our problem is that we have a field only four characters wide, and we
>> anticipate that we may have more than 10,000 records to uniquely
>> identify in this field. We need to autoincrement this field.
>> Many, many TIA, CC.
>>

NC> use Math::BaseCnv;
NC> my \$hex = \$ARGV[0];
NC> my \$num = cnv( \$hex, 16, 10 );
NC> \$num++;
NC> \$hex = cnv( \$num, 10, 16 );

ever heard of the hex() and oct() functions in perl?

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

Re: autoincrement hex numbers

cartercc@gmail.com <cartercc@gmail.com> wrote:

> Can Perl autoincrement hex numbers?

Pfft!

What's with that wimpy base-16 when base-26 is free for the taking?

perl -le '\$a='a'; print \$a++ for 1 .. 1000'

--
Fort Worth, Texas

 cartercc@gmail.com 04-18-2006 11:49 AM

Re: autoincrement hex numbers

Many thanks for all the help.

I used Math::BaseCnv. The lines that do the work are as follows:

\$counter = 0; #integral value
\$key = sprintf("%04s", cnv(++\$counter));

\$counter gets saved in a text file to save the accumulated the values
between invocations, and \$key gets written into rows for an upload
file.

CC

 Nico Coetzee 04-18-2006 04:35 PM

Re: autoincrement hex numbers

Uri Guttman wrote:
>>>>>> "NC" == Nico Coetzee <blabla@yourguess.com> writes:

>
> NC> cartercc@gmail.com wrote:
> >> Can Perl autoincrement hex numbers?
> >> Our problem is that we have a field only four characters wide, and we
> >> anticipate that we may have more than 10,000 records to uniquely
> >> identify in this field. We need to autoincrement this field.
> >> Many, many TIA, CC.
> >>

>
> NC> use Math::BaseCnv;
> NC> my \$hex = \$ARGV[0];
> NC> my \$num = cnv( \$hex, 16, 10 );
> NC> \$num++;
> NC> \$hex = cnv( \$num, 10, 16 );
>
> ever heard of the hex() and oct() functions in perl?
>
> uri
>

Never use it - this was a quick search on CPAN :)

 Uri Guttman 04-18-2006 06:59 PM

Re: autoincrement hex numbers

>>>>> "NC" == Nico Coetzee <blabla@yourguess.com> writes:

NC> Uri Guttman wrote:
>>>>>>> "NC" == Nico Coetzee <blabla@yourguess.com> writes:

NC> cartercc@gmail.com wrote:
>> >> Can Perl autoincrement hex numbers?
>> >> Our problem is that we have a field only four characters wide, and we
>> >> anticipate that we may have more than 10,000 records to uniquely
>> >> identify in this field. We need to autoincrement this field.
>> >> Many, many TIA, CC.
>> >> NC> use Math::BaseCnv;

NC> my \$hex = \$ARGV[0];
NC> my \$num = cnv( \$hex, 16, 10 );
NC> \$num++;
NC> \$hex = cnv( \$num, 10, 16 );
>> ever heard of the hex() and oct() functions in perl?
>> uri
>>

NC> Never use it - this was a quick search on CPAN :)

why would you search cpan before you even searched perl's docs? modules
are for the things that are not builtin to perl. hex and octal
conversion are core things perl does just fine without a module.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

 Nico Coetzee 04-18-2006 07:20 PM

Re: autoincrement hex numbers

Uri Guttman wrote:
>>>>>> "NC" == Nico Coetzee <blabla@yourguess.com> writes:

>
> NC> Uri Guttman wrote:
> >>>>>>> "NC" == Nico Coetzee <blabla@yourguess.com> writes:

> NC> cartercc@gmail.com wrote:
> >> >> Can Perl autoincrement hex numbers?
> >> >> Our problem is that we have a field only four characters wide, and we
> >> >> anticipate that we may have more than 10,000 records to uniquely
> >> >> identify in this field. We need to autoincrement this field.
> >> >> Many, many TIA, CC.
> >> >> NC> use Math::BaseCnv;

> NC> my \$hex = \$ARGV[0];
> NC> my \$num = cnv( \$hex, 16, 10 );
> NC> \$num++;
> NC> \$hex = cnv( \$num, 10, 16 );
> >> ever heard of the hex() and oct() functions in perl?
> >> uri
> >>

>
> NC> Never use it - this was a quick search on CPAN :)
>
> why would you search cpan before you even searched perl's docs? modules
> are for the things that are not builtin to perl. hex and octal
> conversion are core things perl does just fine without a module.
>
> uri
>

Do you have a point? Or do you just want some acknowledgment?

 Dr.Ruud 04-18-2006 08:12 PM

Re: autoincrement hex numbers

cartercc@gmail.com schreef:

> [Math::BaseCnv]
> \$counter = 0; #integral value
> \$key = sprintf("%04s", cnv(++\$counter));

\$key = sprintf '%04x', ++\$counter;

See `perldoc -f sprintf`.

--
Affijn, Ruud

"Gewoon is een tijger."

All times are GMT. The time now is 07:37 AM.