Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 26th, 2001, 02:50 AM
underwarez underwarez is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2000
Location: toronto, ontario, canada
Posts: 10 underwarez User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old July 26th, 2001, 02:52 AM
underwarez underwarez is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2000
Location: toronto, ontario, canada
Posts: 10 underwarez User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Also, how can I generate a random string? For fields in my table (database) that are strings.

Reply With Quote
  #3  
Old July 26th, 2001, 05:01 AM
robert.swift's Avatar
robert.swift robert.swift is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Location: Manchester, UK
Posts: 80 robert.swift User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
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.

Reply With Quote
  #4  
Old July 26th, 2001, 08:37 AM
Hero Zzyzzx's Avatar
Hero Zzyzzx Hero Zzyzzx is offline
11
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jul 2001
Location: Lynn, MA
Posts: 4,635 Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 23 h 44 m 19 sec
Reputation Power: 77
Send a message via AIM to Hero Zzyzzx
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > generating random strings


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT