Inputs: ConvertSeconds(no. of seconds)
Returns: $days,$hours,$minutes,$seconds
Assumes: 100% Accurate
CODE
sub ConvertSeconds {
my $secs=shift;
if($secs<(60*60*24)){
$days=0;
}
else{
$days=int($secs/(60*60*24));
}
if((int($secs%60*60*24))<60*60){
$hours=0;
}
else{
$hours=int( ($secs%(60*60*24) ) / (60*60));
}
if( int(($secs % (60*60*24)) % (60*60)) < 60 ){
$minutes=0;
$seconds=int(($secs % (60*60*24)) % (60*60));
}
else{
$minutes=int((($secs%(60*60*24))%60*24)/60);
}
return $days,$hours,$minutes,$seconds;
}
Replace function
This is a simple script that shows how to create a Replace Subroutine and call it from a script more easily by supplying 3 parameters
EX: &Replace($FullString, $SearchThis, $ReplaceWithThis);
CODE
use strict;
my $strString = '';
my $strSearch = '';
my $strReplace = '';
my $strFinal = '';
print 'Enter a string: ';
chomp ($strString = <STDIN>);
print 'Enter a Search: ';
chomp ($strSearch = <STDIN>);
print 'Enter a Replace: ';
chomp ($strReplace = <STDIN>);
$strFinal = &Replace($strString, $strSearch, $strReplace);
print "$strFinal\n";
sub Replace {
my $strString = shift;
my $strSearch = shift;
my $strReplace = shift;
$strString =~ s/$strSearch/$strReplace/ge;
return $strString;
}
Split Function
This is a simple script that shows how to make a simple Split subroutine that can be called more easy from a script by just supplying 2 parameters.
EX: &Split($fullString, $splitString);
CODE
use strict;
my $strString = '';
my $strSplit = '';
my @strFinal = ();
print 'Enter a string: ';
chomp ($strString = <STDIN>);
print 'Enter a string to Split on: ';
chomp ($strSplit = <STDIN>);
@strFinal = &Split($strString, $strSplit);
foreach(@strFinal) {
print "$_\n";
}
###########################################
## Subroutine to a string based on another
## Character or Character String
###########################################
sub Split {
my $strString = shift;
my $strSplit = shift;
my @words = ();
@words = split /$strSplit/, $strString;
return @words;
}
my $strString = '';
my $strSplit = '';
my @strFinal = ();
print 'Enter a string: ';
chomp ($strString = <STDIN>);
print 'Enter a string to Split on: ';
chomp ($strSplit = <STDIN>);
@strFinal = &Split($strString, $strSplit);
foreach(@strFinal) {
print "$_\n";
}
###########################################
## Subroutine to a string based on another
## Character or Character String
###########################################
sub Split {
my $strString = shift;
my $strSplit = shift;
my @words = ();
@words = split /$strSplit/, $strString;
return @words;
}
Arrays
Shows you how to read an array one by one. Then it shows you how to search an array for a word and add it to a new array. In this example it searchs DSM cars from an array and adds them to a new array twice.
CODE
@carz = ("DSM Talon", "DSM Laser", "Nissan Skyline", "Mazda RX7", "Toyota Supra", "DSM Lancer Evo");
@new = (""); #This is going to become the new array
foreach $carz(@carz){ #Grabs each item out of the array
print "$carz\n";#Prints the grabbed Item
push(@new, $carz); #Adds the Item to the new array
if ($carz =~/DSM/){#If it the car is a "DSM" then it_
print "$carz\n"}#Adds it to the array twice.
push(@new, $carz); #this is just the car being added a second time
}
print "\n\n\n";#setting in some blank spaces
foreach $new(@new){#showing off the new array!
print"$new\n";
}
@new = (""); #This is going to become the new array
foreach $carz(@carz){ #Grabs each item out of the array
print "$carz\n";#Prints the grabbed Item
push(@new, $carz); #Adds the Item to the new array
if ($carz =~/DSM/){#If it the car is a "DSM" then it_
print "$carz\n"}#Adds it to the array twice.
push(@new, $carz); #this is just the car being added a second time
}
print "\n\n\n";#setting in some blank spaces
foreach $new(@new){#showing off the new array!
print"$new\n";
}


