Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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

by Anonymous Monk
on May 02, 2013 at 12:28 UTC ( #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 (Chancellor) 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 (Parson) 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 (Prior) 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 (Abbot) 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 (Parson) 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
Node Status?
node history
Node Type: perlquestion [id://1031745]
Approved by marto
help
Chatterbox?
[choroba]: but gurus say they in fact happen left to right, but we understand them wrongly
[marioroy]: bad gurus :)
[choroba]: also, &> redirects both at the same time
[marioroy]: erix are you using csh
Discipulus uh the link was for cmd.exe the bash one
[marioroy]: If yes, for redirection to work, one might do the following
[marioroy]: `bash -c "clang --version 2>/dev/null"`
[choroba]: Sane people don't use csh
[marioroy]: the 1>&2 will work, not shown
[marioroy]: sorry, did not know which if cmd.exe or unix (bash)

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2017-03-23 08:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (284 votes). Check out past polls.