Honesty Rocks! truth rules.

## Few Perl Snippets For Misc Things

### imacul8

Convert any number of seconds to days,hours,minutes and seconds. Useful for converting unix time differences into days,hours,minutes and seconds.

Inputs: ConvertSeconds(no. of seconds)

Returns: \$days,\$hours,\$minutes,\$seconds

Assumes: 100% Accurate

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);

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);

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; }

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.

@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"; }

### slu

Convert any number of seconds to days,hours,minutes and seconds. Useful for converting unix time differences into days,hours,minutes and seconds.

Inputs: ConvertSeconds(no. of seconds)

Returns: \$days,\$hours,\$minutes,\$seconds

Assumes: 100% Accurate

sub ConvertSeconds { ... }

Your code does not work. Depending on the final if-statement \$seconds might be undefined. A much simpler version would use the standard localtime() funtion:

sub ConvertSeconds { my (\$secs, \$minutes, \$hours, \$days) = localtime(shift); return \$days-1,\$hours-1,\$minutes,\$secs;}