in reply to Searching Strings for Characters
To strip spaces from the end of the string:
$string =~ s/\s+$//;I recall that someone showed a while back that it is more efficient to run these two statements sequentially rather than combining them into one statement like:
$string =~ s/^\s+|\s+$//g;To find out more about regular expressions, you should be able to run the command perldoc perlre to see what your version of Perl has to offer.
To find out whether anything is left in the string you just have to do something like:
if( length($string) > 0 ) { # do something }
If you want to start chopping up strings, substr is your friend.
if( length($string) > 50 ) { # cut the string and either add ... to show it was truncated. $string = substr( $string, 0, 47 ) . '...'; # alternatively, or not $string = substr( $string, 0, 50 ); }
Seeing how you have used 50 in two places, it is good practice to put the 50 in a variable and then refer to that. That way, when you decide that you want to cut strings at 45 or 60 characters, you only have to change your code in one place.
my $MAXLEN = 50; if( length($string) > $MAXLEN ) { # cut the string and either add ... to show it was truncated. $string = substr( $string, 0, $MAXLEN-3 ) . '...'; # alternatively, or not $string = substr( $string, 0, $MAXLEN ); }
I personally would do a use constant (i.e. use constant MAXLIM => 50) but it is a fact that it gives a lot of people the heebie-jeebies around here, mainly because interpolating a constant into string is ugly.
|
|---|