LinuxQuestions.org
Help answer threads with 0 replies.
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 09-02-2008, 04:57 AM   #1
activeq
Member
 
Registered: Jul 2006
Location: Balen, Belgium
Distribution: Suse 10, Centos, Open Solaris
Posts: 76

Rep: Reputation: 15
addings columns in perl script


Hi,

First of all..i'm a beginner in perl scripting.

I have an output-file like this:

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.

Last edited by activeq; 09-02-2008 at 04:59 AM.
 
Old 09-02-2008, 09:47 AM   #2
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 718

Rep: Reputation: 72
Hi.

If you use CODE tags your intent may be more visible to us.

Select the text in the editing window, then click the # just above the window. That will surround the text with the appropriate tags
Code:
like     this
a   b   c   d
cheers, makyo
 
Old 09-02-2008, 12:08 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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.
Code:
    printf( "%30s %-30s start %25s", $name, $phone, $startTime );
--- rod.
 
Old 09-02-2008, 04:26 PM   #4
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,460

Rep: Reputation: 98
printf is enough for this case, but perl format should be mentioned imho, else why use perl
Code:
my($name, $telephone, $stime);

format Infos =
@<<<<<<<<<<<<<<<<<<@<<<<<<<<<<<<<<<<<<<@<<<<<<<<<<<<<<<
$name,             $telephone,         $stime
.

$~ = 'Infos';

($name, $telephone, $stime) = ('Name:', 'Telephone:', 'Start Time:');
write;

($name, $telephone, $stime) = ('Mike Bax', 'tel-41596', 'Fri 8/22 13:31');
write;

Last edited by keefaz; 09-02-2008 at 04:28 PM.
 
Old 09-02-2008, 05:28 PM   #5
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 718

Rep: Reputation: 72
Hi.

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
Best wishes ... cheers, makyo
 
Old 09-03-2008, 01:17 AM   #6
activeq
Member
 
Registered: Jul 2006
Location: Balen, Belgium
Distribution: Suse 10, Centos, Open Solaris
Posts: 76

Original Poster
Rep: Reputation: 15
Thanks for your replies.

I think I know how to do this for my setup, and the solution of Keefaz is the easiest one i think. Thanks for that.

Regards.
 
  


Reply

Tags
columns


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
Script to cycle over two columns using entries from each as vars in same command no_treble Programming 6 10-19-2007 07:54 AM
Shell - Accessing COLUMNS in a script!? BlueSpirit Programming 3 06-04-2007 12:22 PM
[Perl] append columns to file noir911 Programming 3 02-08-2007 05:29 AM
sed script to read only columns 4 to 6 in output database cranium2004 Programming 10 02-28-2006 07:20 AM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 10:22 AM


All times are GMT -5. The time now is 09:17 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration