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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
11-16-2006, 10:03 PM
|
#1
|
LQ Newbie
Registered: Nov 2005
Posts: 11
Rep:
|
Aligning columns by a character
Hey guys
I'm trying to lineup my columns by the 35th character, which will be a decimal.
I don't think printf will help me, my partner in crime thinks he can do it in a loop with some funky code, I think there must be an easier solution.
Here's what we're trying to get.
35.0
1985.1
2.36
So the decimals are all lined up which is the 35th character in each line.
Any ideas?
|
|
|
11-17-2006, 03:33 AM
|
#2
|
Member
Registered: Nov 2006
Location: horten, norway
Distribution: gentoo / slackware / suse / red hat
Posts: 30
Rep:
|
I don't understand exactly what you're trying to do.
Can you explain it more clear?
Are you trying to start the next column from 35th character or?
mARGO
|
|
|
11-17-2006, 03:50 AM
|
#3
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
try man cut
examples would be nice.
a loop with funky code won't be needed.
|
|
|
11-17-2006, 08:40 AM
|
#4
|
LQ Newbie
Registered: Nov 2005
Posts: 11
Original Poster
Rep:
|
Yeah I'm trying to align 3 columns at the 35th character.
I tried to show it on here but it didn't come through.
Should sort of look like this, hopefully it works this time.
.................................3.7
................................78.9
...............................657.10
See how the decimals are lined up? Thats what I'm trying to do. Fortunately I have a few weeks to figure it out.
I'll check out the cut command and see if that helps me.
|
|
|
11-17-2006, 09:29 AM
|
#5
|
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 11,139
|
Let me give a clue, instead of the answer, in case this is homework...
What is the position of the decimal-point character in the string?
Figure out how many spaces to emit before the string.
Remember to consider that the string might begin with some spaces of its own...
----
Another strategy:
It's two substrings, now isn't it? One to the left of the decimal; the other to the right....
|
|
|
11-17-2006, 10:14 AM
|
#6
|
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530
Rep:
|
What's wrong with printf?
Code:
#!/usr/bin/perl -w
my @numbers = (3.7, 79.9, 657.1);
foreach my $n (@numbers) {
printf("%34s%s\n", "", $n);
}
|
|
|
11-17-2006, 10:59 AM
|
#7
|
Member
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377
Rep:
|
Quote:
Originally Posted by shmuck
.................................3.7
................................78.9
...............................657.10
See how the decimals are lined up?
|
No, we don't see it, because you forgot the [code] markup again. BTW, this example is worse than the first because in 657.10 you're printing a not significant 0.
Let's assume you forgot the zeros in 3.70 and 78.90.
In that case, you want printf format "%f37.2" -- 34 digits, the period, and 2 digits, for a total of 37.
If you're using Perl, you may want to consider format --  perldoc
Quote:
Originally Posted by perlform
you may also use "#" characters (with an optional ".") to specify a numeric field. This way you can line up the decimal points.
|
|
|
|
All times are GMT -5. The time now is 06:02 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
|
|