views:

487

answers:

6

I have a output file which is a two dimensional array (this file was output generated after running script written to produce 2D array) and I have to read information under a particular column, say column 1. In other words, how do I read and print out information listed, corresponding to all the rows, under column 1.

Any suggestions?

__DATA__
   1  2  3  4  5  6  7  8  9
   A  B  C  D  E  F  G  H  I
  93  48 57 66 52 74 33 22 91

From the above data I want to extract information column wise, say if I want information from column 1, I should be able to list only the following output. want to list Then I want

OUTPUT:

1
A
93
+2  A: 
Sinan Ünür
@Sinan: Your code is valid if I would have a 2D array and want to print out information in columns, but no I don't instead I have an output file. I have a file which has information in the form of two dimensional array (i.e. it is an output(file) of a script written to print out a 2D array). I have to read this file and extract information. And, I only want to extract the values listed, say under column 1. hopefully that makes sense.
shubster
As Telemachus says, show what the input looks like.
Sinan Ünür
@Sinan: Input shown.
shubster
@Sinan: Can you explain " print +(split)[$column_to_show]; " with a comment. thanks.
shubster
@Sinan Unur: I read both the documentations. I know split function and have also used it many a times. What I didnt know was 'split[column_to_print]'. Do square brackets signify something special? Why does 'split()' give error?
shubster
@Sinan: and I am learning a lot from your answers. just wanted to say thanks :).
shubster
@shubster You are welcome, but read carefully .. split() and (split) are two different things. Square brackets, as always, hold the index. The code you are asking about is equivalent to: **`my @result = split; print $result[$column_to_show], "\n";`**.
Sinan Ünür
+1  A: 

Try playing with this code. Basically I load the data into an array of arrays

Each line is a reference to a row.

#!/usr/bin/perl

use strict;
use warnings;    

my $TwoDimArray;
while (my $line=<DATA>) {
   push @$TwoDimArray, [split(/,/,$line)];
};

for my $column (0..2) {
   print "[$column,0] : " . $TwoDimArray->[0]->[$column] ."\n";
   print "[$column,1] : " . $TwoDimArray->[1]->[$column] ."\n";
   print "\n";
}

__DATA__
1,2,3,04,05,06
7,8,9,10,11,12
lexu
A: 

You can access them directly by element.

$arrays[0][0] = 1;
$arrays[0][1] = 2;
$arrays[1][0] = 3;
$arrays[1][1] = 4;

for (my $i = 0; $i <= $#{$arrays[1]}; $i++) {
    print "row for $i\n";
    print "\tfrom first array: " . $arrays[0][$i] . "\n";
    print "\tfrom second array: " . $arrays[1][$i] . "\n";
}

prints

row for 0
    from first array: 1
    from second array: 3
row for 1
    from first array: 2
    from second array: 4
10rd_n3r0
A: 

The map function is your friend:

open FILE, "data.txt";
while ($line = <FILE>) {
   chomp($line);
   push @data, [split /[, ]+/, $line];
}
close FILE;

@column1 = map {$$_[0]} @data;

print "@column1\n";

And in data.txt something like:

1, 2, 3, 4
5, 6, 7, 8
9, 10, 11, 12
13, 14, 15, 16
Eric
+1  A: 
perl -lne '@F = split /\s+/ and print $F[1]'
ephemient
The manual `@F=...` was based on an interpretation of the input from an earlier version of the question. With the new, better-specified input, `perl -alne 'print $F[0]'` is enough.
ephemient
+1  A: 

This might be what you want:

use English qw<$OS_ERROR>; # Or just use $! 
use IO::Handle;

my @columns;

open my $fh, '<', 'columns.dat' or die "I'm dead. $OS_ERROR";
while ( my $line = <$fh> ) { 
    my @cols = split /\s+/, $line;
    $columns[$_][$fh->input_line_number()-1] = $cols[$_] foreach 0..$#cols;
}
$fh->close();
Axeman
+1 for preempting my snarky comment about `English`. ;-)
Sinan Ünür
It looks emphatic too. :D
Axeman