Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Is this the correct structure?

by hermes1908 (Novice)
on Jul 04, 2013 at 18:44 UTC ( #1042504=note: print w/replies, xml ) Need Help??

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:
  • 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
#!/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";

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1042504]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2018-06-20 07:19 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.