Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Few Perl Snippets For Misc Things
imacul8
post Aug 20 2006, 03:02 AM
Post #1


Member [Level 3]
******

Group: Members
Posts: 95
Joined: 21-May 06
From: Adelaide, Australia
Member No.: 24,017



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


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";
    }
Go to the top of the page
 
+Quote Post
cjm1504
post Oct 19 2006, 01:07 AM
Post #2


Newbie [Level 1]
*

Group: Members
Posts: 10
Joined: 19-October 06
Member No.: 31,874



very helpful Thanks
Go to the top of the page
 
+Quote Post
slu
post Jan 9 2007, 10:07 AM
Post #3


Newbie [Level 2]
**

Group: Members
Posts: 27
Joined: 22-June 05
Member No.: 8,557



QUOTE(imacul8 @ Aug 20 2006, 04:02 AM) *

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
CODE
    
    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:

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

Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Perl Help(1)
  2. First Perl Programm(5)
  3. Second Perl Programm(0)
  4. Perl Programms(0)
  5. Perl Programming(0)
  6. Using Mysql With Perl(1)
  7. Perl V.s. Php(5)
  8. Easy Authenication With Cgi/perl(0)
  9. Perl Progamming/scripting(3)
  10. Perl/cgi Help!(3)
  11. Perl Interview Questions(1)
  12. Compiling Perl / Python?(3)
  13. Xmlhttprequest And Perl(2)
  14. Matching Accents In Perl Regular Expresions(1)
  15. Call C Code From Perl(1)
  1. What Is Perl ? For A Layman Like Me !(7)
  2. Where Do I Install Perl Scripts?(6)
  3. Perl As A Way To Program Desktop Applications(8)
  4. Perl Interactive Mode(2)


 



- Lo-Fi Version Time is now: 27th July 2008 - 01:16 AM