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 practice (prevents autovivification)
my (@x, @y1, @y2, @y3, @y4);
open FILE, '<:crlf', "test.txt";
#Get and store header data
my @headerdata=split /\t/, $headerline;
#Process each subsequent line of the file, storing the current line in
#Remove newline from $_ (var holding current line)
#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;
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!
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:
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||