Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Searching Strings for Characters

by Anonymous Monk
on Jun 04, 2002 at 02:04 UTC ( [id://171380]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have created a form that will cut off the string if it reaches over 50 characters long, but if someone were to press spacebar 50 times, the string will become 50 white spaces and this is a problem.
So here are a few solutions I have decided on (if you have a better one please tell),
1.) I could try to detect a string with empty characters in it an no characters at all
2.) Cut off all empty spaces before the actual word and all the empty spaces after the acutal word
3.) Find out a way to find if a string is just empty
*any others?

if you guys don't get my question just post, thanks a lot, perl monks are "uber"

Replies are listed 'Best First'.
Re: Searching Strings for Characters
by Zaxo (Archbishop) on Jun 04, 2002 at 02:15 UTC

    There is a magical form of split which will do all that:

    $data = join ' ', split " ", $data;
    That's all it takes. length($data) will be 0 if the original was all spaces.

    After Compline,
    Zaxo

Re: Searching Strings for Characters
by mfriedman (Monk) on Jun 04, 2002 at 02:15 UTC
    What exactly are you looking for in the string? All of the things you mentioned are fairly easy to detect:

    String with all whitespace:

    if ($string =~ /^\s+$/)

    Or, to get rid of whitespace in the beginning of a string:

    $string =~ s/\s+(\w.*)/$1/;
Re: Searching Strings for Characters
by Cyrnus (Monk) on Jun 04, 2002 at 03:20 UTC
    Something like
    $string =~ s/^\s+/ /; ($string) = $string =~ /^\s?(.{0,50}\w+)/; if (length $string > 50) { $string =~ s/\w+$//; }
    The first line removes all multiple spaces and replaces them with a single space. The second line ignores a leading space then capures 50 characters and if a word extends past the 50 word boundary it catches the rest of the word as well. The if/then block removes that last word if the length of the string is over 50 characters. Keep in mind if someone enters 51 (or more) characters in a row with no spaces then $string will have an empty value at the end of this block of code.

    Edit: In response to your reply  $string =~ s/^\s+/ / breaks down as:
    =~ apply the regular expression on the right of this operator to the string on the left.
    s/^\s+/ / substitute one or more spaces at the beginning of the string with a single space. It would have been better in this case to leave out the space between the last two slashes, but I'm used to using s/\s+/ /g which replaces all multiple spaces with a single space.

    The code I posted will do the two things you mentioned as well as cut the string down to 50 characters or less without truncating the last word (If just a couple letters of the last word goes over the 50 character limit then the whole word goes) You can't depend on the truncating being done on client side. There are too many ways of getting around client side content control.

    John
      Hi i'm the poster, basically there are 2 types of things i want to fix...

      First Case:

      "(pretend there are 10 or so white spaces) some input(pretend there are 5 or so white spaces)"

      I want it to be:
      "some input"
      and also

      Second Case:

      " (pretend this is a lot of white spaces) "
      I want this to be:
      "(pretend this has no spaces in it)"


      also as a side note can you explain to me what does =~ s/^\s+/ /; means I don't get this at all, I am trying to transfer from C++ thats why I have so much trouble. Thanks again guys, you guys are the best.
Re: Searching Strings for Characters
by grinder (Bishop) on Jun 04, 2002 at 12:38 UTC
    To strip spaces from the beginning of the string:

    $string =~ s/^\s+//;

    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.


    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://171380]
Approved by rob_au
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-20 00:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found