LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   addings columns in perl script (http://www.linuxquestions.org/questions/programming-9/addings-columns-in-perl-script-667040/)

activeq 09-02-2008 04:57 AM

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.

makyo 09-02-2008 09:47 AM

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

theNbomr 09-02-2008 12:08 PM

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.

keefaz 09-02-2008 04:26 PM

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;


makyo 09-02-2008 05:28 PM

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

activeq 09-03-2008 01:17 AM

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.


All times are GMT -5. The time now is 12:27 AM.