I wrote my self this subroutine... which comes in very handy :-) 

#
# strip
#

=head2 strip

=over

=item Description

Strips out leading and training whitespaces from references... 

=item Input

  * Reference to an array, hash or string
    or
    A string

=item Return

  * If input was a reference then, None - The reference passed as input is modified... 
    Else, the stripped string

=item Method Type

 * This method can be used as a class or object method

=back

=cut

sub strip {
  my $self = shift;
  my $ref  = shift;

  if (! ref($ref)) {
    $ref =~ s/(^[\s\t]*)|([\s\t]*$)//g if (defined $ref);
    return $ref;
  }
  elsif (ref($ref) =~ /^ARRAY$/i) {
    foreach my $i (0 .. $#$ref) {
      $ref->[$i] =~ s/(^[\s\t]*)|([\s\t]*$)//g;
    }
  }
  elsif (ref($ref) =~ /^HASH$/i) {
    while (my ($key, $value) = each %$ref) {
      delete $ref->{$key};
      $key         =~ s/(^[\s\t]*)|([\s\t]*$)//g;
      $value       =~ s/(^[\s\t]*)|([\s\t]*$)//g;
      $ref->{$key} = $value;
    }
  }
  elsif (ref($ref) =~ /^SCALAR$/i) {
    $$ref =~ s/(^[\s\t]*)|([\s\t]*$)//g;
  }
  else {
    die "Unknown reference type";
  }
}


On Wed, 2003-10-15 at 15:28, Sudarshan Raghavan wrote:
> [EMAIL PROTECTED] wrote:
> 
> > Can someone hlpe me clean up this trim?
> >
> > Rule: remove all trailing blanks and newline/LF
> 
> perldoc -q 'How do I strip blank space from the beginning/end of a
> string'
> 
> 
> >
> >
> > Do I need a chomp here somewhere?
> 
> No, the \s+ will take care of that
> 
> 
> >
> >
> > sub trim
> > { my $z = $_[0];
> >
> >   $z =~ s/^\s+//;
> 
> You don't need the above statement if you only need to remove trailing
> blanks and newlines
> 
> 
> >
> >   $z =~ s/\s+$//;
> >
> >   return $z;
> > }
> 
> You could maybe write the sub as
> sub trim {
>     s/\s+$// foreach (@_);
> }
> 
> This will also trim an entire list if needed
> 
> 
> >
> >
> > thanks,
> > -rkl


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to