Created
April 10, 2013 20:00
-
-
Save jdittmarCodeSnippets/5357937 to your computer and use it in GitHub Desktop.
Perl: trimErroneousCharactersAtEnds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################################# | |
# subroutine to trim off the white space, carriage returns, pipes, and commas from both ends of each string or array | |
sub trimErroneousCharactersAtEnds { | |
my $guy = shift; | |
return if !defined $guy; | |
my $type = (ref(\$guy) eq 'REF') ? ref($guy) : ref(\$guy); | |
if ( $type eq 'ARRAY') { # Reference to an array | |
foreach(@{$guy}) { #for each element in @{$guy} | |
s/\s+$//g; | |
s/,+$//g; | |
s/[\s+|\r+|\015\012+|\012+|\n+|,+]$//g; #replace one or more spaces at the end of it with nothing (deleting them) | |
s/\s+^//g; | |
s/,+^//g; | |
s/^[\s+|\r+|\015\012+|\012+|\n+|,+]//g; #replace one or more spaces at the beginning of it with nothing (deleting them) | |
} | |
} | |
elsif ( $type eq 'SCALAR' ) { # Reference to a scalar | |
$guy=~ s/\s+$//g; | |
$guy=~ s/\r+$//g; | |
$guy=~ s/\n+$//g; | |
$guy=~ s/\012+$//g; | |
$guy=~ s/\015\012+$//g; | |
$guy=~ s/,+$//g; | |
$guy=~ s/^\s+//g; | |
$guy=~ s/^\r+//g; | |
$guy=~ s/^\n+//g; | |
$guy=~ s/^\012+//g; | |
$guy=~ s/^\015\012+//g; | |
$guy=~ s/^,+//g; | |
} | |
return $guy; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment