CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
length() does not count square brackets as 2 chatacters

 



ahfeii
Novice

Jan 15, 2013, 6:05 PM

Post #1 of 4 (852 views)
length() does not count square brackets as 2 chatacters Can't Post

Dear Sir/Madam,

I have a string:
$string = "Hello [Alfred], how are you?";
print length($string); // result: 28

The result supposes to be 30, because [ and ] considered as 2 characters each as both [ and ] are the extended characters in GSM format.
May I know is there any perl functions can count characters more efficiently?
Or I should create my own function personally?


BillKSmith
Veteran

Jan 15, 2013, 8:17 PM

Post #2 of 4 (847 views)
Re: [ahfeii] length() does not count square brackets as 2 chatacters [In reply to] Can't Post

There are exactly 28 characters in the string. There is nothing special about the square brackets. Perhaps you expect the quotes to be part of the string. They are not. They are the syntax which tells perl that everything between them is a string.

If you really need any other interpratation, I suppose that you would have to write your own, but I strongly urge you to conform to perl conventions.
Good Luck,
Bill


ahfeii
Novice

Jan 15, 2013, 10:03 PM

Post #3 of 4 (844 views)
Re: [BillKSmith] length() does not count square brackets as 2 chatacters [In reply to] Can't Post

Hi Bill,
Noted with thanks!


7stud
Enthusiast

Jan 15, 2013, 11:35 PM

Post #4 of 4 (843 views)
Re: [ahfeii] length() does not count square brackets as 2 chatacters [In reply to] Can't Post

Why do you use an example where counting is so difficult?


Code
 
use strict;
use warnings;
use 5.012;

my $str = 'a';
say length $str; #output is 1

What conclusions can you draw from that?


Code
$str ="[1]"; 
say length $str; #output is 3



(This post was edited by 7stud on Jan 15, 2013, 11:37 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives