create columns from lines in perl

D

drivera

Hello,

Can someone in Perl show me to read lines, convert each line
into columns. I know how to OPEN input files and write to TARGETs but
not sure how to get my desired output accomplished.
Below is a sample text file I am working with.
I want to display each line as a vertical column.

10/13/05 801503 0 1 98 0
10/14/05 789367 0 1 98 0
10/15/05 824447 0 1 99 0
10/16/05 774423 4 1 95 0
10/17/05 765487 2 2 96 0
10/18/05 796621 1 1 98 0
10/19/05 795507 0 1 98 0
10/20/05 786567 0 1 98 0
10/21/05 792068 0 1 98 0
10/22/05 820183 0 1 99 0
10/23/05 770143 4 1 95 0
10/24/05 716063 4 2 94 0
NOTE: these are space delimited fields.

Thank You in advance
 
U

usenet

drivera said:
I want to display each line as a vertical column.

10/13/05 801503 0 1 98 0
10/14/05 789367 0 1 98 0
[snip snip snip]

You want to do what??? You want to display the info like:

10/13/05 10/14/05 [snip snip snip]
801503 789367
0 0
1 1
98 98
0 0

Do I understand your question correctly?
 
D

drivera

Hi David,

yes that is exactly what I am trying to do. I have seen different
postings (example: 'format' )but still not clear to me.

Thanks,

Dino
 
U

usenet

drivera said:
yes that is exactly what I am trying to do.

Well, something like this would work. It uses an array of arrays to
organize the data so you can print it in the order you want. I'm using
__DATA__ but you can create a filehandle and use it in place of <DATA>:


#!/usr/bin/perl
use strict; use warnings;

my @data;
push @data, [split (/\s+/, $_)] for <DATA>;

foreach my $row(0..5) {
foreach my $col(0..(@data-1)) {
printf("%-9s", $data[$col][$row]);
}
print "\n";
}

__DATA__
10/13/05 801503 0 1 98 0
10/14/05 789367 0 1 98 0
10/15/05 824447 0 1 99 0
10/16/05 774423 4 1 95 0
10/17/05 765487 2 2 96 0




Output:

10/13/05 10/14/05 10/15/05 10/16/05 10/17/05
801503 789367 824447 774423 765487
0 0 0 4 2
1 1 1 1 2
98 98 99 95 96
0 0 0 0 0
 
A

Anno Siegel

drivera said:
yes that is exactly what I am trying to do.

Well, something like this would work. It uses an array of arrays to
organize the data so you can print it in the order you want. I'm using
__DATA__ but you can create a filehandle and use it in place of <DATA>:


#!/usr/bin/perl
use strict; use warnings;

my @data;
push @data, [split (/\s+/, $_)] for <DATA>;

Using map and the default action of split:

my @data = map [ split] said:
foreach my $row(0..5) {
foreach my $col(0..(@data-1)) {

(@data-1) would be better written $#data. You mean the highest index
to the array, not the number one less than the number of its elements.
printf("%-9s", $data[$col][$row]);
}
print "\n";
}

The method shows clearly how the problem is related to matrix transposition,
considering the data a two-dimensional array. The other part of the problem
(printing the data in columns) you have solved using a sufficiently wide
printf format.

Incidentally, the Text::Table module is able to do both. The transposition
part is done by specifying entire columns as multiline data. All columns
are added to the table at once:

use Text::Table;

my @data = <DATA>;
tr/ /\n/s for @data; # form multiline columns
print Text::Table->new()->add( @data);

__DATA__
10/13/05 801503 0 1 98 0
10/14/05 789367 0 1 98 0
10/15/05 824447 0 1 99 0
10/16/05 774423 4 1 95 0
10/17/05 765487 2 2 96 0

Output:
10/13/05 10/14/05 10/15/05 10/16/05 10/17/05
801503 789367 824447 774423 765487
0 0 0 4 2
1 1 1 1 2
98 98 99 95 96
0 0 0 0 0

Anno
 
D

drivera

Thanks to all for responding. In order to use Text: Table, I need to
load the module first which doesn't appear to be installed.

For the sake of time, I have taken David's response and got it to
display how I want it. NOW, I just need to know how to get it to write
the output to a filehandle instead of to standard out.
I have tried placing the TARGET filehandle in several locations but it
disrupts the formatting.

CODE:
#!/bin/perl
#### this script will take input of file (acquired from sar.out) and
redirect output to columns
open (HANDLE, "<horizontal.out");
open (TARGET, ">datasar.out") or die ;

my @data;
push @data, [split (/\s+/, $_)] for <HANDLE>;


foreach my $row(0..12) {
foreach my $col(0..(@data-1)) {
printf("%-9s", $data[$col][$row]);
}
print "\n";
}

close (TARGET);
close (HANDLE);
 
E

Eric Schwartz

drivera said:
For the sake of time, I have taken David's response and got it to
display how I want it. NOW, I just need to know how to get it to write
the output to a filehandle instead of to standard out.

You need to spend a bit of time learning how to use the perl
documentation on your computer. Many people here, including those
that are likely to help you the most, get annoyed at being asked
questions that could be answered in less time than it takes to compose
a USENET post simply by consulting your own computer.

I suggest you try

perldoc perl - this will tell you all the other resources perldoc
can show you

perldoc perltoc - this will give you all the FAQ questions. You can
get the answers by using 'perldoc -q <phrase>' where
<phrase> is some important phrase in the question.

You can get documentation on any installed perl modules with

perldoc module::name # for instance, "perldoc IO::Handle"

You can also find the documentation for any built-in perl function
with

perldoc -f funcname

In this case,

perldoc -f printf

would tell you:

printf FILEHANDLE FORMAT, LIST
printf FORMAT, LIST

I assume you can figure it out from there.

-=Eric
 
D

drivera

thanks again to everyone's responses (as I said in the beginning of my
thread, it's been a few years since I've used Perl hence my questions).

Anyway, here is how I got this to print out to my output filehandle
"TARGET"

foreach my $row(0..12) {
foreach my $col(0..(@data-1)) {
printf (TARGET "%-9s", $data[$col][$row]) ;
}
print TARGET "\n" ;
 
T

Tad McClellan

drivera said:
foreach my $col(0..(@data-1)) {


This would be a less error-prone way of doing the same thing:

foreach my $col ( 0 .. $#data ) {



Whitespace is not a scarse resource, feel free to use as much of
it as you want to help make your code easier to read.
 
J

Joe Smith

drivera said:
I just need to know how to get it to write
the output to a filehandle instead of to standard out.

1) Put the handle where it needs to go
or
2) Use select();

my $old_output_handle = select TARGET;
print "This goes to the file TARGET is associated with.\n";
select $old_output_handle;
print "Back to STDOUT or whatever.\n";

-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,948
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top