################################################################################ # # Program: perl_lib1.pl # Author: Peter R. Schmidt # Description: Perl Utility Functions # # INDEX OF AVAILABLE UTILITY SUBROUTINES: # # sub clipped (string) remove trailing blanks # sub rm_preceeding (str) remove preceeding blanks # sub skip_line (number) skip X lines on the screen # sub center (string,length to center in) center a string # sub duration (seconds) get duration from # of seconds # sub lower_case (str) force to lower case # sub upper_case (str) force to upper case # sub get_expected (filename) get expected # of records(wc -l) # sub mysubstr (str) My version of substr # sub pause () Press to continue # sub rm_commas (str) remove commas # sub fix_sign (str) Make a negative # actually neg # sub catmatter (clientpart, matterpart,spacer) Combine client/matter # ################################################################################ # REMOVE TRAILING BLANKS sub clipped { local ($str) = @_; local ($c); $len = length($str); for ($i = $len; $i != 0; $i--) { $c = substr($str,$i,1); if ($c eq "\n") { next }; # SKIP NEWLINES if ( ($c ne " ") && ($c ne "") ) { last }; } $i++; $str = substr($str,0,$i); } ################################################################################ # REMOVE PRECEEDING BLANKS sub rm_preceeding { local ($str) = @_; local ($c); $len = length($str); for ($i = 0; $i <= $len; $i++) { $c = substr($str,$i,1); if ( ($c ne " ") && ($c ne "") ) { last }; } $str = substr($str,$i,$len); } ################################################################################ # SKIP (newline) X LINES ON THE SCREEN sub skip_line { local ($x) = @_; for ($i = 0; $i <= $x; $i++) { print "\n" }; } ################################################################################ # CENTER A STRING sub center { # ARG 1 = STRING, ARG2 = LENGTH TO CENTER STRING IN local ($STR) = $_[0]; local ($OUTLEN) = $_[1]; $STRLEN = length($STR); if ($STRLEN >= $OUTLEN) { $RETSTR = substr($STR,1,$OUTLEN); return ($RETSTR); } $STARTP = ( ($OUTLEN - $STRLEN) / 2); $RETSTR = " " x $STARTP . $STR; } ################################################################################ # DURATION sub duration { # ARG 1 = NUMBER OF SECONDS, RETURNS HOURS,MINUTES,SECONDS local ($SECONDS) = $_[0]; local ($MINUTES) = 0; local ($HOURS) = 0; if ($SECONDS >= 60) { $MINUTES = int($SECONDS / 60); $TMP = ($MINUTES * 60); $SECONDS = ($SECONDS - $TMP); } if ($MINUTES >= 60) { $HOURS = int($MINUTES / 60); $TMP = ($HOURS * 60); $MINUTES = ($MINUTES - $TMP); } if ($SECONDS == 1) { $RET_STR = "$SECONDS second"; } else { $RET_STR = "$SECONDS seconds"; } if ($MINUTES > 0) { if ($MINUTES == 1) { $RET_STR = "$MINUTES minute, " . $RET_STR; } else { $RET_STR = "$MINUTES minutes, " . $RET_STR; } } if ($HOURS > 0) { if ($HOURS == 1) { $RET_STR = "$HOURS hour, " . $RET_STR; } else { $RET_STR = "$HOURS hours, " . $RET_STR; } } $RET_STR; } ################################################################################ # FORCE TO lower CASE sub lower_case { local ($str) = @_; $str =~ tr/A-Z/a-z/; $str; } ################################################################################ # FORCE TO UPPER CASE sub upper_case { local ($str) = @_; $str =~ tr/a-z/A-Z/; $str; } ################################################################################ # GET RECORD COUNT - EXPECTED NUMBER OF RECORDS INPUT FILE sub get_expected { local ($filename) = @_; local ($rec_cnt); open (WC,"wc -l $filename | tail -1 | cut -d' ' -f1 |") || die "cannot do a word count on $filename"; while () { chop; $rec_cnt = $_; } close (WC); $rec_cnt } ################################################################################ # Pause sub pause { local ($str); print "Press to continue:\n"; $str = ; } ################################################################################ # GET SUB-STRING, EXPECT STRING LENGTH SMALLER THEN AMOUNT OF SUB-STRING # TURNS OUT THIS ROUTINE IS PROBABLY NOT NECESSARY # substr SEEMS TO HANDLE REFERENCES PAST THE STRING LENGTH JUST FINE sub mysubstr { local ($str) = $_[0]; local ($offset) = $_[1]; local ($dlength) = $_[2]; local ($i); local ($c); local ($retstr); $strlen = length($str); $position = ($offset + 1); $retstr = ""; for ($i=0; $i<$dlength; $i++) { if ($position > $strlen) { last }; # EXCEEDS STRING LENGTH $c = substr($str,$i+$offset,1); $retstr = $retstr . $c; $position++; } $retstr; } ################################################################################ # REMOVE COMMAS FROM A STRING sub rm_commas { local ($str) = @_; $str =~ tr/,//d; $str; } ################################################################################ # MAKE A NEGATIVE NUMBER ACTUALLY NEGATIVE UNDER PERL sub fix_sign { local ($num) = @_; if ( ($num =~ /[()-]/) || ($num =~ /CR/) ) { $num =~ tr/-//d; $num =~ tr/(//d; $num =~ tr/)//d; $num =~ tr/CR//d; $num = sprintf ("%.2f",($num * -1)); } $num; } ################################################################################ # COMBINE A CLIENT NUMBER WITH A MATTER NUMBER WITH A CHARACTER BETWEEN sub catmatter { local ($clientpart,$matterpart,$spacer) = @_; $clientpart .= $spacer; $matter = $clientpart . $matterpart; $matter; #RETURNS CONCATENATED MATTER } ################################################################################ 1; # LAST LINE MUST CONTAIN A RETURN VALUE OF 1 - DO NOT REMOVE!!! ###############################################################################