#!/usr/bin/ #-- try: #!/usr/bin/perl #-- or: #!/usr/bin/env perl #program to generate 6 Million unique codes, Oct 15, 2011. #-- 'use strict;' - strongly recommended #-- 'use warnings;' - strongly recommended open(MYOUTFILE, ">codesSixMil.txt"); #open for write, overwrite (>) #-- 1.) 3-argument-open() recommended #-- 2.) filehandle could be e.g. $codesfile instead of MYOUTFILE, but it's # okay for a small script. #-- 3.) check if open fails: # 'open( MYOUTFILE, '>', "codesSixMil.txt") or die "cannot open codesSixMil.txt - $!";' #-- 4.) maybe use a variable for filename: 'my $code_filename = "codesSixMil.txt" #-- 'use strict;' will tell you to rewrite the following lines # as 'my $count = 0;' etc. $count = 0; #keep track of how many codes generated $count1 = 0; #keep track of how many codes duplicated #-- bad variable names, esp. $count1 --> e.g. $codes_generated, $codes_unique $desiredQuantity = 6000001; #-- 'off by one' smell - if you want exactly 6M then write 6000000 and adjust loop #-- (or more readable: 6_000_000) @arrayOfResults =(); #-- bad variable name: suggestion: @unique_codes or @raw_codes or ... sub generate_random_string { my $stringsize = shift; my @alphanumeric = ('a'..'z', 3,4,6,7,9); my $randstring = join '', (map { $alphanumeric[rand@alphanumeric] } @alphanumeric)[0 .. $stringsize]; #-- maybe: # my $randstring = join '', (map { $alphanumeric[rand@alphanumeric] } (1..$stringsize); return $randstring; } for ($i=0;$i<$desiredQuantity;$i++) { #-- C-style loop. More Perlish: 'for (1..$desiredQuantity) {' $returnvalue = generate_random_string(10); #-- bad name. Suggestion: $code_key or the like... #-- maybe use a $code_length variable instead of '10'? push(@arrayOfResults, $returnvalue); $count++; #-- not very useful: after the loop: '$count == $desiredQuantity;' #-- Can be replaced by $desiredQuantitity (maybe). } #remove any potential duplicates @unique = grep { ++$count{$_} < 2 } @arrayOfResults; #-- waste of memory, but should work #loop through array to output each code on its own line #with a carriage return as required. for ($i=0;$i<$desiredQuantity;$i++) { #-- needless use of C-style loop here #-- if you have duplicates, @unique < @arrayOfResults, so #-- replace $desiredQuantity by $#unique - or better #-- rewrite the loop in 'for my $code ( @unique ) {' style print @unique[$i], "\n"; #-- for debugging? Increases runtime. print MYOUTFILE @unique[$i], "\r\n"; $count1++; #-- see $count above } #do some rudimentary checking and output result. print "\n---Code generation report--- \n\n"; print "First array: $count \n"; print " Unique array: $count1 \n\n"; print "---There are ", $count-$count1, " code duplicates in this listing of ", $desiredQuantity, " ---\n\n\n"; #-- Your current implementation will always tell you: $count1 == $count == $desiredQuantity #-- even if @unique < @arrayOfResults giving you the false impression to have $count unique codes #*** Close the file *** close(MYOUTFILE);