Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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";

In reply to Re: Is this the correct structure? by hermes1908
in thread Is this the correct structure? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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:
    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
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (20)
    As of 2014-08-29 15:33 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (281 votes), past polls