How can I use Perl extract a particular column from a tab-separated file?

前端 未结 4 1063
灰色年华
灰色年华 2020-12-12 05:57

I am really new at Perl and have been trying to piece together a solution for this. When I run this program I don\'t get any errors and it doesn\'t display anything.

4条回答
  •  一生所求
    2020-12-12 06:26

    Try this out:

    use strict;
    use warnings;
    use Data::Dumper;
    use List::MoreUtils qw;
    
    my $column = first_index { $_ eq 'Ball' } split /\t/, ;
    say Data::Dumper->Dump( [ $column ], [ '*column' ] );
    my @balls  = map { [split /\t/]->[$column] } ;
    say Data::Dumper->Dump( [ \@balls ], [ '*balls' ] );
    __DATA__
    Camera Make Camera Model    Text    Ball    Swing
    a   b   c   d   e
    f   g   h   i   j
    k   l   m   n   o
    

    You would pretty much have to change the handle from DATA to some file you open-ed.

    open( my $in, '<', '/path/to/data.file' ) 
        or die "Could not open file: $!"
        ;
    

    And then substitute for <$in>.

提交回复
热议问题