kamal
kamal

Reputation: 9785

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.2612450122833
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

Upvotes: 0

Views: 577

Answers (2)

wickie79
wickie79

Reputation: 470

Split will be the simplest way

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

Upvotes: 0

codaddict
codaddict

Reputation: 455440

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.

Upvotes: 5

Related Questions