Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
12-10-2009, 12:01 AM
#1
Member
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215
Rep:
Lenght of a number?
How can I get the lenght of a number?For example if my number is 123456789 I should get the output as 9.
12-10-2009, 12:10 AM
#2
Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
Assuming the number is a string variable (or for this purpose, make it one), the length of a string can be found using the
echo ${#variable} syntax. Check the bash man page for this and many other neat string things
shell$ man bash
Sasha
12-10-2009, 12:16 AM
#3
LQ Newbie
Registered: Dec 2008
Location: bangalore,india
Distribution: debian lenny
Posts: 13
Rep:
perl liner
Quote:
Originally Posted by
ashok.g
How can I get the lenght of a number?For example if my number is 123456789 I should get the output as 9.
hello . use a perl one-liner in your script as
Code:
perl -wln -e 'print length($_);
or
num=$(echo $number | perl -wln -e 'print length($_);)
there you go
regards.
12-10-2009, 12:31 AM
#4
Senior Member
Registered: Aug 2006
Posts: 2,695
Quote:
Originally Posted by
ashok.g
How can I get the lenght of a number?For example if my number is 123456789 I should get the output as 9.
Code:
$ number=123456789
$ length=${#number}
$ echo $length
12-10-2009, 12:33 AM
#5
Senior Member
Registered: Aug 2006
Posts: 2,695
Quote:
Originally Posted by
gaurav1086
hello . use a perl one-liner in your script as
Code:
perl -wln -e 'print length($_);
or
num=$(echo $number | perl -wln -e 'print length($_);)
there you go
regards.
that's one of the many ways, but calling an external command is unnecessary and adds overhead.
12-10-2009, 12:49 AM
#6
Member
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215
Original Poster
Rep:
Quote:
Originally Posted by
ghostdog74
Code:
$ number=123456789
$ length=${#number}
$ echo $length
Here I am getting the output as :
Code:
bash: ${#$n}: bad substitution
12-10-2009, 12:54 AM
#7
Member
Registered: Jun 2008
Posts: 31
Rep:
Code:
mo@mo-laptop:~/python$ number=12345
mo@mo-laptop:~/python$ length=${#number}
mo@mo-laptop:~/python$ echo $length
5
should work.....
12-10-2009, 12:59 AM
#8
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.37, Debian Squeeze
Posts: 7,987
Quote:
Originally Posted by
ashok.g
Here I am getting the output as :
Code:
bash: ${#$n}: bad substitution
What were the commands you entered to get that error?
12-10-2009, 01:00 AM
#9
Member
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215
Original Poster
Rep:
Quote:
Originally Posted by
ashok.g
Here I am getting the output as :
Code:
bash: ${#$n}: bad substitution
Sorry, I used some wrong statement.
It's all working fine. I am getting the desired output.
12-10-2009, 02:58 AM
#10
Senior Member
Registered: Dec 2008
Location: Gurgaon, India
Distribution: Slackware 13.37, OpenSuse 11.3
Posts: 4,373
There is one more way to do it in C:
Go on dividing the number by 10 i.e.
If you divide 1234 by 10, u get the remainder as 4 and quotient as 123.
So what u can do is start a counter which gets incremented each time u get a quotient.
At the end the length of the original number will be counter+1 !
Last edited by Anisha Kaul; 12-10-2009 at 03:00 AM .
Thread Tools
Search this Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT -5. The time now is 04:50 AM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News