LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 11-16-2006, 10:03 PM   #1
shmuck
LQ Newbie
 
Registered: Nov 2005
Posts: 11

Rep: Reputation: 0
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?
 
Old 11-17-2006, 03:33 AM   #2
asquante
Member
 
Registered: Nov 2006
Location: horten, norway
Distribution: gentoo / slackware / suse / red hat
Posts: 30

Rep: Reputation: 15
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
 
Old 11-17-2006, 03:50 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516

Rep: Reputation: 240Reputation: 240Reputation: 240
try man cut

examples would be nice.
a loop with funky code won't be needed.
 
Old 11-17-2006, 08:40 AM   #4
shmuck
LQ Newbie
 
Registered: Nov 2005
Posts: 11

Original Poster
Rep: Reputation: 0
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.
 
Old 11-17-2006, 09:29 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 11,139
Blog Entries: 4

Rep: Reputation: 4103Reputation: 4103Reputation: 4103Reputation: 4103Reputation: 4103Reputation: 4103Reputation: 4103Reputation: 4103Reputation: 4103Reputation: 4103Reputation: 4103
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....
 
Old 11-17-2006, 10:14 AM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 67
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);
}
 
Old 11-17-2006, 10:59 AM   #7
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 32
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
about wide character and multiple byte character George2 Programming 5 05-23-2006 01:03 AM
Aligning output in C++ R00ts Programming 13 02-14-2006 12:51 PM
Need Help With Columns Post Modern Programming 2 02-01-2006 11:18 AM
Trouble aligning heads on HP 3820 inkjet chowsapal Linux - Hardware 1 01-05-2006 08:59 AM
aligning torsmo kurrupt Linux - Software 1 06-28-2005 02:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration