I have a partially sorted text file. Each line has 9 numbers seperated by whitespace. Example:

1 4 6 7 8 9 0 1 0

1 3 4 8 8 9 0 3 2

1 6 7 8 8 8 8 7 1

4 3 6 7 9 1 3 2 1

4 3 4 5 6 7 7 7 7

4 3 4 5 6 3 3 3 7

4 3 4 5 6 3 3 3 7

5 4 6 6 6 6 6 2 4

5 3 4 8 8 9 0 3 2

5 6 7 8 8 8 8 7 1

5 6 7 8 8 8 8 7 1

5 6 7 8 8 8 8 7 1

5 6 7 8 8 8 8 7 1

I need to read each line starting with the same number and put them into a nested array. In this simplified example I need to read all lines starting with 1 and put them into a nested array then later sort this array based on a column. Do the same for all lines starting with the same number. Basically I need to read each line starting with the same number put them in to nested array sort it and then print it, and continue doing th same for the remaining lines that start with the same number. The nested array:

1 4 6 7 8 9 0 1 0

1 3 4 8 8 9 0 3 2

1 6 7 8 8 8 8 7 1

Sorting it based on column 9:

1 4 6 7 8 9 0 1 0

1 6 7 8 8 8 8 7 1

1 3 4 8 8 9 0 3 2

The nested array:

5 4 6 6 6 6 6 2 4

5 3 4 8 8 9 0 3 2

5 6 7 8 8 8 8 7 1

5 6 7 8 8 8 8 7 1

5 6 7 8 8 8 8 7 1

5 6 7 8 8 8 8 7 1

After sorting:

5 6 7 8 8 8 8 7 1

5 6 7 8 8 8 8 7 1

5 6 7 8 8 8 8 7 1

5 6 7 8 8 8 8 7 1

5 4 6 6 6 6 6 2 4

5 3 4 8 8 9 0 3 2

I now How to put lines into a nested array and sort them based on column. My trouble is that I don't know how to read lines starting with the same number and put them into an array then continue doing the same for the rest of the file. Please help! Thanks.


In reply to How can I read multiple lines starting with the same number and put in to a nested array and print it to a file? by opolat

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.