PERL: splitting and reading/accessing the elements in a line
I am trying to build a script to read in lines in a file, SPLIT them by
the tab separator and (eventually add two columns, etc.) But after the
SPLIT command the for (.. does not seem to read the values into the array
@netcolumns. The output file has the line (before the SPLIT) and then a
row of TABs.. but empty.
#!/usr/bin/perl
#
use strict;
use warnings;
use diagnostics;
# input and output files
my $file = "testfile4.txt"; # tab seperated data
my $binout = $file . ".binned.txt";
# open filehandles
open IN, "<$file" or die "cannot open $file: $!\n";
open BOUT, ">$binout" or die "cannot open $binout: $!\n";
# some other variables
my @netcolumns=(); # declare this
# the first line
my $firstLine = <IN>;
print ("$firstLine \n");
print BOUT ("$firstLine \n");
my $netcolumns = split /\t/, $firstLine; # tab seperated data
for (my $j = 0; $j < 7; $j ++) { # print the 7 column headers
from the first line of $input
print BOUT "$netcolumns[$j]\t";
}
print BOUT "\n";
print ("$netcolumns[0] \n");
close IN;
close BOUT;
The screen output shows the first line of the file so $firstLine got the
row from the file. The WARNING I get is the familiar "Use of uninitialized
value within @netcolumns in concatenation (.) or string at bin_data_4.pl
line 26, line 1 (#1) (W uninitialized)"
the data file looks like this:
frame.time_epoch frame.number frame.len ip.src sctp.srcport ip.dst
sctp.dstport 1376065574.000054 1 1514 172.17.78.26 38733 172.16.189.180
3868
1376065574.000054 2 178 172.17.78.26 38733 172.16.189.180 3868
1376065574.000095 3 62 172.16.189.180 3868 172.17.78.26 38733
1376065574.000499 4 410 172.17.78.26 38733 172.16.189.180 3868
1376065574.000536 5 62 172.16.189.180 3868 172.17.78.26 38733
1376065574.000754 6 410 172.17.78.26 38733 172.16.189.180 3868
1376065574.001108 7 410 172.17.78.26 38733 172.16.189.180 3868
1376065574.001149 8 62 172.16.189.180 3868 172.17.78.26 38733
1376065574.001251 9 1514 172.17.78.26 38733 172.16.189.180 3868
1376065574.001251 10 178 172.17.78.26 38733 172.16.189.180 3868
I cannot seem to find the error in line 30 but I imagine it is an obvious
one. Thanks for any help you can offer.
No comments:
Post a Comment