[CSC 330] Perl "Trimmings"

Andrew J. Pounds pounds_aj at mercer.edu
Sun Oct 29 07:15:40 EDT 2023


As some of you are finding out - Perl is VERY powerful and can do some 
tasks in five lines that would take you thirty in another language.  
Sometimes, however, Perl appears to lack things that you would expect to 
be a part of the language -- like a trim function for strings.  This is 
due to the Perl "ethos" and Perl's built-in abilities for manipulating 
strings based on Regular Expression 
<https://en.wikipedia.org/wiki/Regular_expression>.   Here is a small 
Perl function I wrote called alltrim that removes the white spaces from 
both ends of a string using regular expressions.

sub alltrim {
     my $str = $_[0];
     $str =~ s/^\s+|\s+$//g;
     return $str
}

The exercise we did in class with Perl and chomp demonstrated how to 
remov new lines -- alltrim is a more generic way to remove whitespace, 
but it is not a chomp.  You are welcome to use it in your code if you 
need it.

p.s. -- if you use vi/vim for editing, then the s/.....//g regex is the 
same notation you would use for a global substitution.

-- 
*/Andrew J. Pounds, Ph.D./*
/Professor of Chemistry and Computer Science/
/Director of the Computational Science Program/
/Mercer University/
/1501 Mercer University Drive, Macon, GA 31207 /
/(478) 301-5627/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://theochem.mercer.edu/pipermail/csc330/attachments/20231029/c24c88df/attachment.html>


More information about the csc330 mailing list