autoincrement hex numbers

Discussion in 'Perl Misc' started by cartercc@gmail.com, Apr 17, 2006.

  1. Guest

    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.
     
    , Apr 17, 2006
    #1
    1. Advertising

  2. Nico Coetzee Guest

    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 );
     
    Nico Coetzee, Apr 17, 2006
    #2
    1. Advertising

  3. Guest

    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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Apr 17, 2006
    #3
  4. Uri Guttman Guest

    >>>>> "NC" == Nico Coetzee <> writes:

    NC> 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 ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Apr 17, 2006
    #4
  5. <> 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'


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 18, 2006
    #5
  6. Guest

    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
     
    , Apr 18, 2006
    #6
  7. Nico Coetzee Guest

    Uri Guttman wrote:
    >>>>>> "NC" == Nico Coetzee <> writes:

    >
    > NC> 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 :)
     
    Nico Coetzee, Apr 18, 2006
    #7
  8. Uri Guttman Guest

    >>>>> "NC" == Nico Coetzee <> writes:

    NC> Uri Guttman wrote:
    >>>>>>> "NC" == Nico Coetzee <> writes:

    NC> 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 ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Apr 18, 2006
    #8
  9. Nico Coetzee Guest

    Uri Guttman wrote:
    >>>>>> "NC" == Nico Coetzee <> writes:

    >
    > NC> Uri Guttman wrote:
    > >>>>>>> "NC" == Nico Coetzee <> writes:

    > NC> 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?
     
    Nico Coetzee, Apr 18, 2006
    #9
  10. Dr.Ruud Guest

    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."
     
    Dr.Ruud, Apr 18, 2006
    #10
  11. Uri Guttman Guest

    >>>>> "NC" == Nico Coetzee <> writes:

    NC> Uri Guttman wrote:
    >>>>>>> "NC" == Nico Coetzee <> writes:

    NC> Uri Guttman wrote:
    >> >>>>>>> "NC" == Nico Coetzee <> writes:

    NC> 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
    >>


    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

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Apr 18, 2006
    #11
  12. Guest

    > 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
     
    , Apr 19, 2006
    #12
  13. robic0 Guest

    On 18 Apr 2006 16:44:14 -0700, wrote:

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


    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 !!!!!!!!
     
    robic0, Apr 19, 2006
    #13
  14. Nico Coetzee <> wrote:
    > Uri Guttman wrote:
    >>>>>>> "NC" == Nico Coetzee <> writes:

    >>
    >> NC> Uri Guttman wrote:
    >> >>>>>>> "NC" == Nico Coetzee <> writes:

    >> NC> wrote:
    >> >> >> Can Perl autoincrement hex numbers?


    >> >> >> NC> use Math::BaseCnv;


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


    >> NC> Never use it - this was a quick search on CPAN :)
    >>
    >> why would you search cpan before you even searched perl's docs?



    > Do you have a point?



    It seems obvious to me:

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


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 19, 2006
    #14
  15. robic0 Guest

    On Tue, 18 Apr 2006 20:14:58 -0500, Tad McClellan <> wrote:

    >Nico Coetzee <> wrote:
    >> Uri Guttman wrote:
    >>>>>>>> "NC" == Nico Coetzee <> writes:
    >>>
    >>> NC> Uri Guttman wrote:
    >>> >>>>>>> "NC" == Nico Coetzee <> writes:
    >>> NC> wrote:
    >>> >> >> Can Perl autoincrement hex numbers?

    >
    >>> >> >> NC> use Math::BaseCnv;

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

    >
    >>> NC> Never use it - this was a quick search on CPAN :)
    >>>
    >>> why would you search cpan before you even searched perl's docs?

    >
    >
    >> Do you have a point?

    >
    >
    >It seems obvious to me:
    >
    > You should look for a builtin before you look for a module.


    Agree with this. Builtins in the core are at least tested by experts.
     
    robic0, Apr 19, 2006
    #15
  16. Guest

    > 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
     
    , Apr 19, 2006
    #16
  17. Nico Coetzee Guest

    Uri Guttman wrote:
    >>>>>> "NC" == Nico Coetzee <> writes:

    >
    > NC> Uri Guttman wrote:
    > >>>>>>> "NC" == Nico Coetzee <> writes:

    > NC> Uri Guttman wrote:
    > >> >>>>>>> "NC" == Nico Coetzee <> writes:

    > NC> 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
    > >>

    >
    > 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
     
    Nico Coetzee, Apr 19, 2006
    #17
  18. Nico Coetzee Guest

    Tad McClellan wrote:
    > Nico Coetzee <> wrote:
    >> Uri Guttman wrote:
    >>>>>>>> "NC" == Nico Coetzee <> writes:
    >>> NC> Uri Guttman wrote:
    >>> >>>>>>> "NC" == Nico Coetzee <> writes:
    >>> NC> wrote:
    >>> >> >> Can Perl autoincrement hex numbers?

    >
    >>> >> >> NC> use Math::BaseCnv;

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

    >
    >>> NC> Never use it - this was a quick search on CPAN :)
    >>>
    >>> why would you search cpan before you even searched perl's docs?

    >
    >
    >> Do you have a point?

    >
    >
    > 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
     
    Nico Coetzee, Apr 19, 2006
    #18
  19. Nico Coetzee Guest

    wrote:
    >> 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
    >


    AMEN

    Nico
     
    Nico Coetzee, Apr 19, 2006
    #19
  20. Nico Coetzee Guest

    robic0 wrote:
    > On 18 Apr 2006 16:44:14 -0700, wrote:
    >
    >>> 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

    >
    > 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
     
    Nico Coetzee, Apr 19, 2006
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Munnki
    Replies:
    2
    Views:
    539
    Munnki
    Dec 18, 2003
  2. Gene Gorokhovsky
    Replies:
    0
    Views:
    484
    Gene Gorokhovsky
    Jul 17, 2003
  3. RA
    Replies:
    0
    Views:
    363
  4. Replies:
    10
    Views:
    6,408
    Neredbojias
    Aug 19, 2005
  5. Bengt Richter
    Replies:
    6
    Views:
    536
    Juha Autero
    Aug 19, 2003
Loading...

Share This Page