in reply to Is this the correct structure?
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:
Good Luck
Hermes
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";
|
---|
In Section
Seekers of Perl Wisdom