ok, thanks, although i am abit unclear or what the shift does...(the purpose of using shift in the function)
thanks anyway
Main Topics
Browse All TopicsAnyone able to help me? i've got a function in perl which trims spaces (all of them) in a given string(=argument) which looks like :
sub trim {
my($string)=@_;
for ($string) {
s/^\s+//;
s/\s+$//;
}
return $string;
}
it only 'trims' the first spaces and spaces inbetween strings or characters are not deleted, nor are they deleted if the spaces is at the end. any comment is appriciated!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
$ perldoc -f shift
shift ARRAY
shift Shifts the first value of the array off and returns it, short-
ening the array by 1 and moving everything down. If there are
no elements in the array, returns the undefined value. If
ARRAY is omitted, shifts the @_ array within the lexical scope
of subroutines and formats, and the @ARGV array at file scopes
or within the lexical scopes established by the "eval ''",
"BEGIN {}", "INIT {}", "CHECK {}", and "END {}" constructs.
See also "unshift", "push", and "pop". "shift" and "unshift"
do the same thing to the left end of an array that "pop" and
"push" do to the right end.
If you have only one arg, then whether you do
my $arg = shift;
or
my $arg = @_;
is a matter of preference.
By default the arguments to a function are passed in the array named @_. TinTin's shift is shifting the first variable off the array. The following is functionally equivalent:
sub trim {
my $string = shift(@_);
$string =~ s/\s//g;
return $string;
}
The difference is that I explicitly specified the array, but Tintin uses one of Perl's niceties; if the array to shift is not specified, @_ will be used. This may be a little cryptic, but you better get used to it; it is a very common practice. Actually, I see arguments processed Tintin's way more often than I see yours. You have used the same concept in the line
s/^\s+//;
Only that operates on the implicit scalar variable named $_
Lets analyze your regexes to see why they didn't work:
1. s/^\s+//;
Will only remove the white space at the begining of a line
'^' says to match the begining of a line
"\s+" says to match one or more whitespaces
2. s/\s+$//;
Will only remove the white space at the end of a line
"\s+" says to match one or more whitespaces(same as the first regex)
'$' says to match the end of a line
You must have been mistaken when you said that this will not delete whitespace from the end. If you are correct, then there is something you haven't shown us.
Here is how Tintin's regex, "s/\s//g" works: The "\s" says match whitespace, no surprise there, you already used it. The g at the end stands for global. Thus, the regex is applied to the entire line until no more matches occur.
Additional Resources:
http://en.wikipedia.org/wi
Business Accounts
Answer for Membership
by: TintinPosted on 2003-06-06 at 22:39:56ID: 8671684
Your question is a little unclear. Are you just wanting to trim *all* whitespace from a string? If so, use (although you hardly need a function for it)
sub trim {
my $string = shift;
$string =~ s/\s//g;
return $string;
}