Your code has quite a few problems and it seems that you are not grasping some
fundamental concepts. If you are new to the language I would recommend you pick
up a copy of "learning perl". I'm too lazy to comment each line of your code,
but I'm sure some kind monk will. A thing which you have to be careful about
this particular file is that it uses crlf (windows style) newlines. As I
understand it perl automatically accounts for this on windows, but if you're on
unix you need to explicitly open the file with "<:crlf".
Some important concepts you should familiarize yourself with:
- Variable references vs Normal variables (perldata)
- Scalar vs List context (perldata)
- Looping constructs in perl (perlsyn)
- Default variables (perlvar)
The list is by no means complete and as mentioned I would recommend getting
a good book perl (learning perl or programming perl)
Below is some code which does what you want using what I believe
are reasonably good conventions.
Good Luck
Hermes
#!/usr/bin/env perl
#Good practice (prevents autovivification)
use strict;
my (@x, @y1, @y2, @y3, @y4);
open FILE, '<:crlf', "test.txt";
#Get and store header data
chomp(my $headerline=<FILE>);
my @headerdata=split /\t/, $headerline;
#Process each subsequent line of the file, storing the current line in
+ $_
while(<FILE>) {
#Remove newline from $_ (var holding current line)
chomp;
#Split $_ using tab as delimeter and store contents in @rowdata
my @rowdata=split /\t/;
#Store first column of the line in @x, etc..
push @x, shift @rowdata;
push @y1, shift @rowdata;
push @y2, shift @rowdata;
push @y3, shift @rowdata;
push @y4, shift @rowdata;
}
close FILE;
print "Array headerdata: @headerdata\n";
print "Array x: @x\n";
print "Array y1: @y1\n";
print "Array y2: @y2\n";
print "Array y3: @y3\n";
print "Array y4: @y4\n";
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.