<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<p>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 <a
href="https://en.wikipedia.org/wiki/Regular_expression">Regular
Expression</a>. Here is a small Perl function I wrote called
alltrim that removes the white spaces from both ends of a string
using regular expressions.</p>
<p><font face="monospace">sub alltrim {<br>
my $str = $_[0];<br>
$str =~ s/^\s+|\s+$//g;<br>
return $str<br>
}</font><br>
</p>
<p>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>
<p>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. <br>
</p>
<div class="moz-signature">-- <br>
<b><em>Andrew J. Pounds, Ph.D.</em></b><br>
<em>Professor of Chemistry and Computer Science</em><br>
<em>Director of the Computational Science Program</em><br>
<em>Mercer University</em><br>
<em>1501 Mercer University Drive, Macon, GA 31207 </em><br>
<em>(478) 301-5627</em><br>
</div>
</body>
</html>