开发者

How to store values from a column (in a tab-delimited text file) in Perl

Here is an example of the file. Let's say I want to parse the 6th column values and store them in an array.

42  test.example.com    activityViewer/index.html   8a699fe62751d158dad57f6b9a3ae5a4    1291836592  4.1938788890839
4   test.example.com    activityViewer/index.html   ca0317343500ac35d46ca6b6bfe5918d    1291836592  4.224240064621
38  test.example.com    activityViewer/index.html   2a473f02e814896af9d20ad333dfd5c5    1291836592  4.2302849292755
9   test.example.com    activityViewer/index.html   ac3942ad87fb0ce3efb034cdfc3d4c79    1291836592  4.2612450开发者_运维百科122833
31  test.example.com    activityViewer/index.html   3497d90401e18483119b60a378bd8d27    1291836592  4.3139219284058
25  test.example.com    activityViewer/index.html   377b12a86f6bbde33dc89e94cacd5246    1291836592  27.90621805191
3   test.example.com    activityViewer/index.html   493bf2ba361b77d09305a14e9253322d    1291836592  29.722389936447


You can do:

use strict;

my @arr;    
while(<>) {
        my @tmp = split/\t/;
        push @arr,$tmp[5];
}

and call the script as:

$ perl myprg.pl  file

See it

Alternatively you can treat the file as CSV with tab as the field separator and use an suitable CSV parser module.


Split will be the simplest way

my @ary;
foreach my $line (split(/\n/, $full_text)) {
  my @l = split(/\t/, $line);
  push @ary, $l[5]
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜