in reply to Still having problems with spanish characters

also, the  trim() function does not seem correct: it is passed a parameter that is not used, has a useless for-loop, and it alters and returns the  $_ parameter (and i can't figure out what  $_ is at that point). is this really what you intend?

sub trim { for (my $s = $_[0]) { # passed param assigned, not used s/^\s+//; # substitutions on $_ s/\s+$//; return $_; } }

instead, try something like

sub trim { my ($string) = @_; $string =~ s{ \A \s+ }{}xms; $string =~ s{ \s+ \z }{}xms; return $string; }

also also, do not invoke a function with an ampersand, i.e.,  &trim($string);, unless you know what this really does. use something like  trim($string); or  trim("  string to be trimmed  "); instead.

Replies are listed 'Best First'.
Re^2: Still having problems with spanish characters
by almut (Canon) on Sep 28, 2007 at 20:36 UTC
    sub trim { for (my $s = $_[0]) { # passed param assigned, not used s/^\s+//; # substitutions on $_ ...

    Actually, it works... it's only written in a somewhat weird fashion :)

    The passed parameter is assigned to $s, which is then aliased to $_ by the for loop... The seemingly superfluous $s is needed, because otherwise you'd get the error "Modification of a read-only value attempted".  But I agree there are more readable versions...

      it's only written in a somewhat weird fashion

      somewhat weird...?!?!?