OK,

First, I create the @fields array, using the split function with default args (i.e. splits $_ into fields separated by whitespace).

Then, I use the pop function to remove (and get) the rightmost element of the fields array. (push and pop manipulate the right hand side; shift and unshift manipulate the left). So after the $key has been popped, @fields is an array of all fields, except the rightmost.

To push a list onto a list of arrays, I need an array reference. I could have used \@fields; but there are subtle ways that can go wrong when you're new to perl data-structures. So instead, I create a new anonymous array reference (using square brackets).

Later, when I want convert the array-reference back into an array, I need to use the array dereferencing operator: @{$line}. I appologise for using a standard shorthand here: perl interprets @$line as @{$line}.

Your last question is how to format you colmns more nicely. The easiest way (might work) is to join the elements of the array using a tab, instead of a space:

my $text = join("\t", @fields);
If this doesn't work, then there are many alternatives: you could define a format (see books); or you could write some clever padding functions; or you could store the original input text in the hash-of-lines; or you could get a CPAN module such as Data::ShowTable or Text::FormatTable. Personally, I usually don't bother: if I really need to view something as neat tables, then I use a table viewing application (or output as html). --Dave.

In reply to Re: Re: Re: Working With Arrays and Files by dpuu
in thread Working With Arrays and Files by kevinw

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!
  • 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:
    & &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.