skip to main | skip to sidebar

Perl Programming Language Tutorial

Pages

  • Home
 
  • RSS
  • Twitter
Thursday, October 25, 2012

Sepearte First name and last name by using PERL regular expressions

Posted by Raju Gupta at 12:18 PM – 1 comments
 

We are using PERL regular expression.
We are using 3 functions prxparse,prxmatch and prxposn.

Prxparse takes regularexpression and retutns a pattern identification number for the compiled regular expression.

Prxmatch takes a pattern identification numbar and character value and return the position where the egularexpression finds a match

prxposn returns a value for captured buffer.   

 
data Names;
   input name  $32.;
   datalines;
Raj Gupta
Sam Mark
Jenny Derak
John Michel
;
run;

data FirstLastNames;
   length first last $ 16;
   keep first last;
   retain re;
   if _N_ = 1 then
      re = prxparse('/(\w+)\s(\w+)/');
   set Names;
   if prxmatch(re, name) then 
      do;
         last = prxposn(re, 1, name);
         first = prxposn(re, 2, name);
      end;
run;


[ Read More ]
Read more...
Wednesday, October 24, 2012

Validate Info-Perl Script

Posted by Raju Gupta at 9:00 PM – 0 comments
 

It checks whether the length of HTML field values are greater than or equal to some specified minimum length, if not it will report an error. Field name, minimum length are the parameters to this subroutine.   

sub validate_info {
    my ($name,$field, $minlen, $allow_blank) = @_;
    my $count = 0;
    my $errcnt;
    if (length($field) >= $minlen) {
        $errcnt = 0;   ## ok
    } else {
        if ((length($field) == 0) && ($allow_blank eq 'Y')) {
            $errcnt = 0;
        } else {
            &print_message("Minimum length for $name is $minlen","W");
            $errcnt = 1;  ## Counts as an error
        }
    }
    return $errcnt;
}
 

[ Read More ]
Read more...

Print Message--Perl Script

Posted by Raju Gupta at 12:11 PM – 0 comments
 

This is a Subroutine which is called by the scripts where some message is to be printed on the browser. $message and $type are the parameters passed by the scripts. This subroutine formats the $message accordingly to $type. $type indicates the severity of the message by giving some colour value.   

 
sub print_message {

 my ($message, $type) = @_;

 if ($type =~ /E/i) {
  print center(h3("<font color=red size=+2>$message</font>"));
 }
 elsif ($type =~ /W/i) {
  print center(h3("<font color=purple>$message</font>"));
 }
 else {
  print center(h4("<font color=darkblue>$message</font>"));
 }

}


[ Read More ]
Read more...
Tuesday, October 23, 2012

Password encryption-Perl Script

Posted by Raju Gupta at 6:00 PM – 0 comments
 
It is used to encrypt the password. Password to be encrypted is passed as agrument to this subroutine when called.

 
sub encrypt_pass {
 my( $passwd) = @_;
 $passwd = lc($passwd);
 my $salt = substr $passwd, 0, 2;
 my $encryptedpass = crypt($passwd, $salt);
   
    return 1;
}

[ Read More ]
Read more...

Convert to Seconds--Perl script

Posted by Raju Gupta at 12:01 PM – 0 comments
 
It takes time in minutes as parameter and converts it into time in seconds.

 
sub convert_to_seconds {
    my $time_in_seconds = 0;
    my $time_in_minutes = $_[0];

    if ((int($time_in_minutes) <= 0) || (length($time_in_minutes) == 0)) {
         $time_in_minutes = 120;
    }

    $time_in_seconds = int($time_in_minutes) * 60; 
    return $time_in_seconds; 
}

[ Read More ]
Read more...
Monday, October 22, 2012

File size and modified time

Posted by Raju Gupta at 11:56 AM – 0 comments
 
When we pass the file name (include full path) as parameter,It will give us the size of the file and last modified time.

 
sub getFileStats {
 my $filename = $_[0];       ## includes full path
 my $not_applicable = 'N/A';

 # get the stats.  This returns nulls if no file exists
 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat($filename);
 
 if($mtime != undef) {
  # format datetime
  my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime($mtime);  
  $year += 1900;
  if ($min < 10) {
   $min = "0$min";
  }

  $mon = $mon + 1; ## zero based
  
  my $str_time = "$mon/$mday/$year $hour:$min";
  return ($size, $str_time);
 } else {
  return ($not_applicable, $not_applicable);
 }

 #------------------------------------------------------------------- 
 # For reference here are the stats we get for this filename
 #
 # 0 dev      device number of filesystem
 # 1 ino      inode number
 # 2 mode     file mode  (type and permissions)
 # 3 nlink    number of (hard) links to the file
 # 4 uid      numeric user ID of file's owner
 # 5 gid      numeric group ID of file's owner
 # 6 rdev     the device identifier (special files only)
 # 7 size     total size of file, in bytes
 # 8 atime    last access time in seconds since the epoch
 # 9 mtime    last modify time in seconds since the epoch
 # 10 ctime    inode change time (NOT creation time!) in seconds since the epoch
 # 11 blksize  preferred block size for file system I/O
 # 12 blocks   actual number of blocks allocated  
}


[ Read More ]
Read more...

Convert to minute-Perl script

Posted by Raju Gupta at 9:30 AM – 0 comments
 
It takes time in seconds as parameter and converts it into time in minutes.

 
sub convert_to_minutes {
    my $time_in_seconds = $_[0];
    my $time_in_minutes = 0;

    if ((int($time_in_seconds) <= 0) || (length($time_in_seconds) == 0)) {
         $time_in_seconds = 36000;      ## 36000 = 600 minutes  * 60 seconds/minute
    }

    $time_in_minutes = int($time_in_seconds) / 60; 
    return $time_in_minutes; 
}


[ Read More ]
Read more...
Older Posts
Subscribe to: Posts (Atom)
  • Popular
  • Recent
  • Archives

Popular Posts

  • Perl function to compare two dates
    This function can be used to compare two dates using PERL. The function accepts two string(date) arguments, let's say date1 and date2...
  • File size and modified time
    When we pass the file name (include full path) as parameter,It will give us the size of the file and last modified time. sub getFileSt...
  • Perl function to check whether file or dir name passed to it readable or not
    The function makes sure that the path (directory and/or file) passed to it as an Input parameter is readable or not   use constant SU...
  • Perl script to find files older than x minutes
    This script can be used to find files in a windows directory older than 40 min. List can be emailed to a user also. Script can be modi...
  • Sepearte First name and last name by using PERL regular expressions
    We are using PERL regular expression. We are using 3 functions prxparse,prxmatch and prxposn. Prxparse takes regularexpression an...
  • Perl function to check whether the passed path is empty or not
    The function makes sure that the path (directory and/or file) passed to it as an Input parameter is empty or not. use constant SUCCESS ...
  • Perl function to trim leading and trailing spaces from a string
    Leading and trailing spaces, if any present, are trimmed and the string is returned back to the caller. If a NULL string is passed, the func...
  • Fix Message Reader from Log
    Various subroutines of the package FixUtil can be used to read fix message (tag, value pair). Fix message can be extracted. Tag and Value ca...
  • Login screen using Perl
    This code snippet takes one parameter for default user and displays a login screen asking for user name and password . It aslo provides a...
  • Script to rotate any log file
    This script creates a copy of standard out log files of Weblogic Server after it has reached a predefined size limit,renames it with current...
Powered by Blogger.

Archives

  • ▼  2012 (24)
    • ▼  October (24)
      • Sepearte First name and last name by using PERL re...
      • Validate Info-Perl Script
      • Print Message--Perl Script
      • Password encryption-Perl Script
      • Convert to Seconds--Perl script
      • File size and modified time
      • Convert to minute-Perl script
      • Login screen using Perl
      • Huge text file comparator
      • Parse Input - Perl Script
      • Get colored difference - Perl Script
      • Perl function to compare two dates
      • Script to rotate any log file
      • Cross Referencing script
      • Random Bunch Creation in Perl
      • NASDAQ Status checker using Perl
      • Fix Message Reader from Log
      • Perl script to find files older than x minutes
      • Perl function to check whether the passed path is ...
      • Date Arimatic
      • Perl function to trim leading and trailing spaces ...
      • Perl function to check whether the passed path is ...
      • Perl function to check whether file or dir name pa...
      • Perl function to check whether the passed path is ...
 

Followers

Labels

  • File Searching Example (1)
  • Perl Date Example (2)
  • Perl Encryption Example (1)
  • Perl File Example (2)
  • Validation Example (1)
 
 
© 2011 Perl Programming Language Tutorial | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger