ProgrammingThis 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.
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.
Mike Bax tel-41596 start Fri 8/22 13:31
Thomas van Damme tel-92248 start Tue 9/2 8:56
I would like to change this in something like this:
Name: Telephone: Start Time:
Mike Bax tel-41596 Fri 8/22 13:31
Thomas van Damme tel-92248 Tue 9/2 8:56
How can I create columns within Perl?
Note: After submitting this post I see that this forum doesn't include my spaces. I need a columns with Name:, a columns with telephone:, and a seperate columns Start Time.
To attempt to answer your actual question, I suggest looking at the printf()/sprintf() functions, which are good at creating formatted output. In your case, I assume you are interested in padding and justifying text in fixed-width columns, so focus on the use of field-width specifiers.
If your goal is to learn perl, then by all means apply the suggestions provided above and continue to practice writing perl.
If your goal to obtain column alignment, then you may be interested in the results from S Kinzler's perl script:
Code:
#!/bin/bash -
# @(#) s1 Demonstrate Kinzler's perl align script.
# See: http://www.cs.indiana.edu/~kinzler/align/
echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1)
set -o nounset
echo
FILE=${1-data1}
echo " Data file $FILE:"
cat -A $FILE
echo
echo " Results, align default:"
align $FILE
echo
echo " Results, align with options:"
align -s t -j _ -a dddN $FILE
exit 0
Producing with your data, augmented by TAB delimiters and an extra numeric column:
Code:
% ./s1
(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 2.05b.0
Data file data1:
Name:^ITelephone:^IStart Time:^IA number$
Mike Bax^Itel-41596^IFri 8/22 13:31^I43.5$
Thomas van Damme^Itel-92248^ITue 9/2 8:56^I726.129$
Results, align default:
Name: Telephone: Start Time: A number
Mike Bax tel-41596 Fri 8/22 13:31 43.5
Thomas van Damme tel-92248 Tue 9/2 8:56 726.129
Results, align with options:
Name: Telephone: Start Time: A number
Mike Bax tel-41596 Fri 8/22 13:31 43.500
Thomas van Damme tel-92248 Tue 9/2 8:56 726.129
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.