PERL_CODES
use warnings;
sub passwdcrt {
# Description : Creates a random password from a set of given characters
# Function : 1st param = characters, 2nd param = length of password
# Usage : passwdcrt("characters", 8);
# I/O : Input string, number - Output array.
my $input = $_[0];
my $passlength = $_[1];
my @array = split //, "$input";
print " @array \n";
# my $arraysize = @array;
# print "$arraysize \n";
my $anagram = ();
for( my $i=0; $i<$passlength; $i++ ){
my $randlet = $array[ rand @array ];
print "$randlet \n";
push (@anagram, $randlet);
# print @anagram;
}
return @anagram;
}
my @arr = passwdcrt("adfadf2034io234uc2n943c", 5);
print @arr;
sub arrayseq{
# Description : Creates an array and fills it with a number squence
# Function : 1st parameter is the size of the array, 2nd is the increment step
# Usage : arrayseq(5,2)
# I/O : Input parameters : scalar numbers / Output parameters : returns @array
my @arr;
my $current = 0 ;
my $limit = $_[0];
my $incr = $_[1];
for (my $i=0; $i<$limit; $i++ ){
$current += $incr;
# print $i;
$arr[$i] = $current;
# print "@arr \n";
}
return @arr;
}
sub Printlistdelimit{
# Function : Print or Return List elements with added delimiter
# Usage : Printlistdelimit("delimiter", @list)
# Set local variable list = array with all subroutine parameters
my @list = @_;
# Shift removes the first element of an array, making it 1 smaller
# We re doing this to remove the first argument, the delimiter,
# So that we can print only the values that interest us
shift @list;
# To examine values of the list
# print "@list\n";
# Set the delimiter as the first parameter
my $delimiter = $_[0];
# Iterate and print
$i = 1;
foreach my $item (@list){
# print "$item$delimiter";
# Push adds one new value to the right of a list/array
my @output = push @output,"$item$delimiter";
}
# We use return to just return the value
return @output;
# Alternatively we could print to the screen
# print "@output\n";
}
sub PrintList{
# Function : Prints values of an array/list
# Usage : PrintList($nonarray, $array)
# Assign the parameters to a list
my @list = @_;
# Print List
print "Given list is @list\n";
}
@array = arrayseq(5,2);
print "@array \n";
# @array = (1,2,3,4,5)
sub Average{
# Function: Average
# Description: Retuns the mean value
# Input: Straight values or array
# Output: Mean value
# Notes: get total number of arguments passed
# Usage :
# print Average(1, 2, 4);
# print Average(@s);
$n = scalar(@_);
$sum = 0;
foreach $item (@_){
$sum += $item;
}
$average = $sum / $n;
return $average;
}
sub factorial{
$n = $_[0];
$fact = 1;
for (my $i=1; $i<=$n; $i++){
$fact = $fact * $i;
}
return $fact;
}
$sum = Average(10, 20, 30);
print "Average is $sum \n";
$factor = factorial(5);
print "Factorial is $factor \n";