# File: stringGenAlphabet.pl # Title: Sorts a generated a random string of length N (ask for N) # over an alphabet, asks for Alphabet # Author: Mihaela Malita # Generates a random string over an alphabet # Input Alphabet? ACGT # Length of the string(integer)? 10 # Generated string is AGTACCCCAA print "Generates a random string over an alphabet"; print "Input Alphabet (ex. type ACGT)? "; $a = ; chop($a); @Alphabet = split('',$a); #transforms string in array print "Length of your string(integer)? "; $n = ; chop($n); $s = randstring($n); #invokes subroutine print "\nGenerated string is $s"; exit; ##### Subroutine: given length of string makes a random string over the alphabet sub randstring { my($n) = @_; # length of the string result my($s) = ''; # initialize final S with empty '' $len = @Alphabet; # L is a scalar takes value = length of the array for ( $i = 0 ; $i < $n ; $i++) { $pos = int rand($len); # generates random position in alphabet 0 < $pos < L $s = $s . $alphabet[$pos]; # string concatenation with . } return $s; }