Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
P is for Practical
 
PerlMonks  

Unique ID Generator

by LostS (Friar)
on Aug 23, 2001 at 14:33 UTC ( [id://107338]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

LostS has asked for the wisdom of the Perl Monks concerning the following question:

Hey Everyone Again,

OK on today's plate I gotta find a way to do this. I have a ID I must generate. The Formula for this ID is something like "REG-RC-XXXX" Where the X's are those are numbers. So it would be like REG-RC-0001 and so forth... Now I have no problem incrementing... my issue is how do I make it be a 4 digit pattern?? So it will be 0001 and 0201 and so forth??

Any Help you all can give is GREATLY appricated

Thank you all in Advance for the help
Billy S.

Replies are listed 'Best First'.
Re: Unique ID Generator
by scain (Curate) on Aug 23, 2001 at 14:38 UTC
    What? I read your question several times, and still don't understand what you want. Could you please make it a little more clear.

    Scott

    Ahh, thank you, Masem. I get it now (at least I think so, right, LostS?)

      OK I have a ID I have to assign with each submission. The ID looked like this REG-RC-0000 . The 0000 is a number so it would be 0001 and then 0002 and so forth... My Question is how do I make it always 4 characters in length. If I take a number and do a ++ to it wont it change 0001 to 2??

Re: Unique ID Generator
by Masem (Monsignor) on Aug 23, 2001 at 14:39 UTC
    sprintf is your friend:
    my $uniqid = sprintf( "REG-RC-%04d", $id++ );

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    It's not what you know, but knowing how to find it if you don't know that's important

      So using the sprintf will put the $id variable where %04d is located?? Trying to figure out in what order I need to do the read and write of the ID.

      Hey Masem... I tried this out... Can you look at this and see where I went wrong??

      $id = 0; while ($id < 50) { print "Position ID: $positionid<br />\n"; my $positionid = sprintf( "REG-RC-%04d", $id++ ); }

      This is what it printed:

      Position ID: REG-RC-0000
      Position ID: REG-RC-0000
      Position ID: REG-RC-0000
      Position ID: REG-RC-0000
      Position ID: REG-RC-0000
      .....
      Did that 50 times...

      Any Suggestions??

      Billy S.

        Nevermind... I am just dumb and reordered the print statement and the sprintf and it works great :)

        Thank you SOOOO much :)

        Billy S.
        The my declaration is messing things up here, as the value you set here is going to be only local to the block and AFTER the print statement, so it's lost.

        Try this code:

        my $id = 0; my $positionid; while ( $id < 50 ) { $positionid = sprinft( "REG-RC-%04d", $id++ ); print "Position ID: $positionid<br />\n"; }

        -----------------------------------------------------
        Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
        It's not what you know, but knowing how to find it if you don't know that's important

Re: Unique ID Generator
by dga (Hermit) on Aug 23, 2001 at 15:23 UTC

    The one caveat that I can see is that when you run out of your 4 digit number that it will expand to more digits like

    REG-RC-9999 REG-RC-10000

    If you aren't going to have 10000 of these then you should be ok.

    Otherwise you will need to do some stuff like going to REG-RD-0000 after REG-RD-9999.

      Well they came to me and said I could bump it up to a 5 diget number :-P

      If they get 99,999 job positions to fill in the next 4 years... Then DAMN.. That is good right??
Re: Unique ID Generator
by Hofmator (Curate) on Aug 23, 2001 at 16:30 UTC

    I thought I just mention that we have a good tutorial about (s)printf here at PM. You might want to take a look at it - there are more nice things you can do with (s)printf: Using (s)printf()

    -- Hofmator

Re: Unique ID Generator
by John M. Dlugosz (Monsignor) on Aug 23, 2001 at 22:49 UTC
    Try magic increment, too.

    my $ID= "0001"; foreach (1..20) { print "REG-RC-$ID\t"; ++$ID; }
Re: Unique ID Generator
by mitd (Curate) on Aug 24, 2001 at 06:59 UTC
    'Holy Binary Dumps'

    Persistently yours File::CounterFile.

    mitd-Made in the Dark
    'My favourite colour appears to be grey.'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://107338]
Approved by root
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.