Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

How to check if a string contains more than 1 character?

by Anonymous Monk
on May 02, 2013 at 12:28 UTC ( [id://1031745]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks, I have a very basic doubt on the count of characters in a string. I want to do a function if the string contains more than 1 character. So kindly can you give a suggestion on how to check if a string contains more than 1 character. Thanks in advance.
  • Comment on How to check if a string contains more than 1 character?

Replies are listed 'Best First'.
Re: How to check if a string contains more than 1 character?
by choroba (Cardinal) on May 02, 2013 at 12:33 UTC
    sub has_more_than_1_char { my $string = shift; return defined $string && 1 < length $string; }
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: How to check if a string contains more than 1 character?
by karlgoethebier (Abbot) on May 02, 2013 at 13:26 UTC

    Update:

    sub karls_has_more_than_1_char { ( shift =~ /.{2,}/ ) ? 1 : return; } sub karls_has_more_than_1_char_2 { ( shift =~ /(.{2,})/ ) ? $1 : return; }

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: How to check if a string contains more than 1 character?
by hdb (Monsignor) on May 02, 2013 at 12:37 UTC

    Or do you mean two or more different characters?

    use strict; use warnings; my @strings = qw/xxxx xxxyy/; for( @strings ) { print "$_\n" unless /^(.)\1*$/; }
Re: How to check if a string contains more than 1 character?
by InfiniteSilence (Curate) on May 02, 2013 at 13:06 UTC
    I liked the other responses. Here's another.
    perl -e '$str = q|abc|; $str2=q|a|; for($str,$str2){m/\S{2,}/ && prin +t qq|$_|};'

    Celebrate Intellectual Diversity

      You can also put a list of strings directly in the foreach loop.

      perl -e "foreach(qw(abc a 00)){print qq($_\n) if m/\S{2,}/}"
Re: How to check if a string contains more than 1 character?
by graff (Chancellor) on May 03, 2013 at 01:50 UTC
    I'm sorry, but I think the question is too vague. How do you define "1 character"?

    Would you consider ṩ to be one character? How about ẹ̇? (Note the presence of the dot marks above and below each letter.)

    According to the current Unicode Standard (v6.2), the former should be just one character, while the latter should be two characters. (Of course, both could be expressed using as many as three characters.)

Re: How to check if a string contains more than 1 character?
by Laurent_R (Canon) on May 02, 2013 at 15:33 UTC

    How about this one?

    $ perl -e '$string= "f"; $a=7; print "larger than 1 \n" if length $str +ing > ($a*$a - ($a+1)*($a-1));' $ perl -e '$string= "foo"; $a=7; print "larger than 1 \n" if length $s +tring > ($a*$a - ($a+1)*($a-1));' larger than 1 $
Re: How to check if a string contains more than 1 character?
by karlgoethebier (Abbot) on May 02, 2013 at 16:24 UTC

    OK...another (useless?) oneliner:

    Karls-Mac-mini:monks karl$ export foo=b Karls-Mac-mini:monks karl$ echo ${foo} | perl -ne '( /.{2,}/ ) ? exit +1 : exit 0;' Karls-Mac-mini:monks karl$ echo $? Karls-Mac-mini:monks karl$ export foo=bar Karls-Mac-mini:monks karl$ echo ${foo} | perl -ne '( /.{2,}/ ) ? exit +1 : exit 0;' Karls-Mac-mini:monks karl$ echo $?

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-24 20:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found