autoincrement hex numbers

C

cartercc

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.
 
N

Nico Coetzee

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 );
 
X

xhoster

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
 
U

Uri Guttman

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
 
T

Tad McClellan

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'
 
C

cartercc

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
 
N

Nico Coetzee

Uri said:
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 :)
 
U

Uri Guttman

NC> my $hex = $ARGV[0];
NC> my $num = cnv( $hex, 16, 10 );
NC> $num++;
NC> $hex = cnv( $num, 10, 16 );
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
 
N

Nico Coetzee

Uri said:
NC> my $hex = $ARGV[0];
NC> my $num = cnv( $hex, 16, 10 );
NC> $num++;
NC> $hex = cnv( $num, 10, 16 );
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?
 
D

Dr.Ruud

(e-mail address removed) schreef:
[Math::BaseCnv]
$counter = 0; #integral value
$key = sprintf("%04s", cnv(++$counter));

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

See `perldoc -f sprintf`.
 
U

Uri Guttman

NC> my $hex = $ARGV[0];
NC> my $num = cnv( $hex, 16, 10 );
NC> $num++;
NC> $hex = cnv( $num, 10, 16 );
NC> Do you have a point? Or do you just want some acknowledgment?

the point is that you should learn what perl has builtin before
searching cpan. otherwise you will be wasting your time as well as
ours.

and a civil tongue would be helpful too when you are asking for
help. acknowledgements are not required but they are nice to see.

uri
 
C

cartercc

It is good that you found something that works for you, but why use a
non-standard module when basic Perl will do?:

Because we may need the hex number to actually do something, and this
module seemed to present some options that just didn't jump out.
Besides, I actually tried using sprintf with the "%04x" string, but for
some reason it didn't work, and I got the other one to work on the
first try. We have other fish to fry, and after I got it working I
moved on.

CC
 
R

robic0

Because we may need the hex number to actually do something, and this
module seemed to present some options that just didn't jump out.
Besides, I actually tried using sprintf with the "%04x" string, but for
some reason it didn't work, and I got the other one to work on the
first try. We have other fish to fry, and after I got it working I
moved on.

CC

You moved on but your flashlight is only pointing in one direction.
Why don't you come clean whith your mystry usage.

Jesus Christ where do you fuckin assholes come from !!!!!!!!
 
C

cartercc

You moved on but your flashlight is only pointing in one direction.
Why don't you come clean whith your mystry usage.

I am a database manager for a large public university. We are dealing
with a service provider (IBM) for a service that represents a large
chunk of our enrollments, and a hugh chunk of our revenue. Our section
identifiers are five characters long with every character being
significant. IBM's application only has a four character width field.
Unless we get the five characters, WE CANNOT REGISTER STUDENTS!!! This
is supposed to be live in two days, by Friday at open of business. This
isn't the only problem by any means, perhaps the most critical, but not
the only. And it's only temporary, since IBM has ^promised^ an
increased field width ^soon^. We just needed something that worked,
that we could convert back and forth, and potentially use the hex
number as a real number instead of only a key.
Jesus Christ where do you fuckin assholes come from !!!!!!!!

If you mean to be funny, you aren't, but that's okay, because I deal
with a number of people with sour dispositions who don't realize it.
You mean to be serious, I don't like being called a 'fuckin asshole.'
I'm not a programmer, but a database administrator, and I use Perl to
solve problems. I have master's degrees in CS and SE, and a doctorate
(tho' not in CS or SE), and am pretty competent in what I do. Where do
I come from? The real world, where people actually have to solve
problems for a living.

CC
 
N

Nico Coetzee

Uri said:
NC> my $hex = $ARGV[0];
NC> my $num = cnv( $hex, 16, 10 );
NC> $num++;
NC> $hex = cnv( $num, 10, 16 );
NC> Do you have a point? Or do you just want some acknowledgment?

the point is that you should learn what perl has builtin before
searching cpan. otherwise you will be wasting your time as well as
ours.

and a civil tongue would be helpful too when you are asking for
help. acknowledgements are not required but they are nice to see.

uri

At least when a person uses this module, they are not bound by only
hex() and oct() functions, since "BaseCnv can convert between any
possible base between 2 && 64" (from the doc).

You see - sometimes (based on variables not under the programmers
control) specifications doesn't cater for scalability. Please read the
original problem again...

Today they foresee more then 10000, but what if there requirement grows
to beyond 65535? With the module already in place, a small change can
let them grow to several million.

Your suggestion, although perhaps typical of geeks, does not take into
account real life issues, like scalability. In the real world this is a
common problem and this module provide a simple solution that caters for
great scalability out of the box.

I think we all agree that with only 4 characters, you will eventually
run out of options, but at least this module (in this case) can stretch
this limit to a very big figure.

Cheers
 
N

Nico Coetzee

Tad said:
It seems obvious to me:

You should look for a builtin before you look for a module.

Please see my comment to Uri's message regarding scalability.

Even though your suggestions work, they still limit you to a fixed
number (relatively small - still in the thousands), where by using the
module you can increase the maximum count to several million.

In real life applications this is more useful then just sticking to
"university" style best practices (or what ever the latest argument
flavor of the day is).

Cheers
 
N

Nico Coetzee

I am a database manager for a large public university. We are dealing
with a service provider (IBM) for a service that represents a large
chunk of our enrollments, and a hugh chunk of our revenue. Our section
identifiers are five characters long with every character being
significant. IBM's application only has a four character width field.
Unless we get the five characters, WE CANNOT REGISTER STUDENTS!!! This
is supposed to be live in two days, by Friday at open of business. This
isn't the only problem by any means, perhaps the most critical, but not
the only. And it's only temporary, since IBM has ^promised^ an
increased field width ^soon^. We just needed something that worked,
that we could convert back and forth, and potentially use the hex
number as a real number instead of only a key.


If you mean to be funny, you aren't, but that's okay, because I deal
with a number of people with sour dispositions who don't realize it.
You mean to be serious, I don't like being called a 'fuckin asshole.'
I'm not a programmer, but a database administrator, and I use Perl to
solve problems. I have master's degrees in CS and SE, and a doctorate
(tho' not in CS or SE), and am pretty competent in what I do. Where do
I come from? The real world, where people actually have to solve
problems for a living.

CC

AMEN

Nico
 
N

Nico Coetzee

robic0 said:
You moved on but your flashlight is only pointing in one direction.
Why don't you come clean whith your mystry usage.

Jesus Christ where do you fuckin assholes come from !!!!!!!!

Please read my comments to Uri and Tad's messages.

BTW: it's not nice to call people nasty words or to be blasphemous about
it. Some us are actually Christians, and although you might not be I
think it is just fair to respect us and others.

Cheers

Nico
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top