Hi all

I want to parse a text file into an array of array, one line per small array, one letter per element of small array

So if I have a text file containg:

Hello

my

name

is

Jack

The array has to look like this:

@MainArray element 1 ---> elements of small array ---> H, e, l, l, o

@MainArray element 2 ---> elements of small array ---> m, y

@MainArray element 3 ---> elements of small array ---> n, a, m, e

@MainArray element 4 ---> elements of small array ---> i, s

@MainArray element 5 ---> elements of small array ---> J, a, c, k

I have something along these lines, but I don't how to add the part where each new line in the text file is added as a new small array in @MainArray. All this does is add every letter to one large @MainArray

open (A1, "<2 normaal.txt") or die "can't open"; my @MainArray; while (<A1>) { my $in = $_; chomp $in; push (@array1, split(//, $in)); }

Thanks in advance for any help, much appreciated


In reply to Parse txt file into array of array by Dr Manhattan

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.