Search code examples
arraysperldata-structuresperl-data-structures

How to print a particular column from tabular data


I'm Trying to print columns from data by using index key value in the outer part of a foreach loop.

my @col;
foreach(<DATA>){
    @x = split(' ',$_);
@xz = ($x[0],$x[1],$x[2]) ;
    #print "$x[0]\n"; This is working but i'm not expect this.
push(@col,@xz);
} 
print "$col[0]\n";
__DATA__
7       2       3

3       2       8

6       7       2

I expect the output is

7 3 6 

How can i do it?


Solution

  • my @col;
    while (<DATA>) {
        push @col, (split ' ')[0];
        # push @col, /(\S+)/; # split alternative
    }
    print "@col\n";
    
    __DATA__
    7       2       3
    
    3       2       8
    
    6       7       2
    

    output

    7 3 6