Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Generate unique ids of maximum length

by BioLion (Curate)
on Apr 12, 2010 at 17:29 UTC ( [id://834325]=note: print w/replies, xml ) Need Help??


in reply to Generate unique ids of maximum length

I am sure someone will disagree, but i think your approach is fine (apart from the caveats you mention). An alternate approach might be to make a totally unique id, that fits your requrements (I can't remember where i originally found this code - sorry if it is you!):

sub generate_random_string { my $length_of_randomstring = shift; # the length of the random +string to generate my @chars=('a'..'z','A'..'Z','0'..'9','_'); my $random_string; foreach (1..$length_of_randomstring) { # rand @chars will generate a random # number between 0 and sc +alar @chars $random_string.=$chars[rand @chars]; } return $random_string; }

Or is should imagine there are random id generators on CPAN...

And associate that with the readable id using a lookup table or similar(I assume this is why you want to keep them "as similar as possible"?). You can then convert between the two for printing purposes, but for storage, all your entries have a proper (but non-sensical) random and unique id.

Just my opinion, but i hope it helps...

Just a something something...

Replies are listed 'Best First'.
Re^2: Generate unique ids of maximum length
by almut (Canon) on Apr 12, 2010 at 17:55 UTC

    Why a random string and not simply a sequence number?  The latter would have the advantage of definitely being unique (besides being trivial to implement), while a random string might (in theory) repeat, and thus not necessarily be "totally unique"... (which means you'd need to check against a hash holding already used IDs).  Also, for the reverse lookup a simple array would suffice.

      Good points - especially the array part - I was just thinking about the 'strings of the same length' part. Either way, the point is the same.

      Just a something something...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://834325]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-16 18:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found