|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
generating random strings
I will like to generate records to import into a data base. One of the primary keys for my database is a string of length 3, so how can generate the following list of strings?
aaa aab aac ... aaz aba abb ... zzz After I find out how to generate the strings. I will print the list in a file and import into the database. Thanks in advance |
|
#2
|
|||
|
|||
|
Also, how can I generate a random string? For fields in my table (database) that are strings.
|
|
#3
|
||||
|
||||
|
Check out http://forums.devshed.com/showthrea...18061&forumid=6
To generate your sequence of alpha's try: Code:
#!/usr/bin/perl -w
use strict;
my ($f, $s, $t) = (0, 0, 0);
my $n = (26*26*26);
#
sub process_string {
print @_, "\n";
}
#
# loop until $t == $s == $f == 26
#
until ($n-- == 0) {
my $string;
$string = pack("C*", (97 + $t), (97 + $s), (97 + $f));
process_string($string);
if ($f++ > 24) {
$f = 0;
if ($s++ > 24) {
$s = 0;
$t++;
}
}
}
I hope this helps.
__________________
Robert. |
|
#4
|
||||
|
||||
|
This is how I generate random strings. Works pretty well.
Code:
sub rand_string{
my $length=shift;
my @seed=qw(b c d f g h j k l m n p q r s t v w x y z 2 3 4 5 6 7 8 9);
my $cartid ='';
foreach(1..$length){
$cartid.=$seed[rand(@seed)];
}
return $cartid;
}
Basically, put the seed values you want into the @seed array. You can then create strings as long as you want by calling this sub with a number as an argument- Code:
my $id = rand_string(3); Would create 3 character random string in the $id scalar, using the @seed array for the values. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > generating random strings |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|