mmartin has asked for the wisdom of the Perl Monks concerning the following question:


Hell Monks,

I was wondering if someone could help me in figuring this out (Splitting one array into a 2D-Array)?
I thought it was something as simple as the following but it didn't work: (can't figure out the logic)
# @records contains the data from the file. for (my $x = 0; $x <= $#records; $x++) { for (my $y = 0; $y <= ; $y++) { $AoA[$x][$y] = split ' ', $records[$x]; } }


I have a file that I read into my script and assign each line of that file into an array. So now I have an array in which each element in that array now contains a single line from that file.
So my question is now how do I split that single array into a 2D-Array by splitting each line on whitespace.

The file and Array Contains this: each line is an element in the array
OWNER /prod-data/J 00151120273 X jmorg 3584038 247 ts/49 13:38:12 Jul +20 WAITING /prod-data/J 00151120273 X jmorg 2015244 134 s/109 13:48:32 Ju +l 20 WAITING /prod-data/J 00151120273 X gdavi 1359996 62 ts/20 13:54:22 Jul + 20 OWNER /prod-data/J 001!L!311895 X jmorg 2015244 134 s/109 13:48:32 Jul + 20 WAITING /prod-data/J 001!L!311895 X jmorg 5713932 191 ts/46 14:01:42 J +ul 20 OWNER /prod-datahi 001!10274882 X rfuse 3354796 61 ts/43 13:39:02 Jul +20 WAITING /prod-datahi 001!10274882 X jmorg 3584038 247 ts/49 13:39:22 J +ul 20

So if I do the following:
print "$records[0]\n"; I get printed to the screen:
 OWNER /prod-data/J 00151120273 X jmorg 3584038 247 ts/49 13:38:12 Jul 20

I would like to be able to say:
print "$AoA[0][0]\n";
_Output_
OWNER


Any help would be greatly appreciated.

Thanks, Matt

Replies are listed 'Best First'.
Re: How to split a single array into a two-dimensional array?
by ikegami (Patriarch) on Aug 01, 2011 at 20:31 UTC
    my @AoA; for my $x (0..$#records) { $AoA[$x] = [ split ' ', $records[$x] ]; }
    or
    my @AoA; for (@records) { push @AoA, [ split ]; }
    or
    my @AoA = map { [ split ] } @records;
Re: How to split a single array into a two-dimensional array?
by BrowserUk (Patriarch) on Aug 01, 2011 at 20:31 UTC

    Just map split over the array:

    [0] Perl> @data = ("AAA BBB CCC DDD EEE FFF GGG") x 10;; [0] Perl> pp \@data;; [ "AAA BBB CCC DDD EEE FFF GGG", "AAA BBB CCC DDD EEE FFF GGG", "AAA BBB CCC DDD EEE FFF GGG", "AAA BBB CCC DDD EEE FFF GGG", "AAA BBB CCC DDD EEE FFF GGG", "AAA BBB CCC DDD EEE FFF GGG", "AAA BBB CCC DDD EEE FFF GGG", "AAA BBB CCC DDD EEE FFF GGG", "AAA BBB CCC DDD EEE FFF GGG", "AAA BBB CCC DDD EEE FFF GGG", ] [0] Perl> @data = map[ split ], @data;; [0] Perl> pp \@data;; [ ["AAA", "BBB", "CCC", "DDD", "EEE", "FFF", "GGG"], ["AAA", "BBB", "CCC", "DDD", "EEE", "FFF", "GGG"], ["AAA", "BBB", "CCC", "DDD", "EEE", "FFF", "GGG"], ["AAA", "BBB", "CCC", "DDD", "EEE", "FFF", "GGG"], ["AAA", "BBB", "CCC", "DDD", "EEE", "FFF", "GGG"], ["AAA", "BBB", "CCC", "DDD", "EEE", "FFF", "GGG"], ["AAA", "BBB", "CCC", "DDD", "EEE", "FFF", "GGG"], ["AAA", "BBB", "CCC", "DDD", "EEE", "FFF", "GGG"], ["AAA", "BBB", "CCC", "DDD", "EEE", "FFF", "GGG"], ["AAA", "BBB", "CCC", "DDD", "EEE", "FFF", "GGG"], ]

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: How to split a single array into a two-dimensional array?
by mmartin (Monk) on Aug 02, 2011 at 14:20 UTC


    Hey Guys,

    Thanks for your replies. Each one worked perfectly!


    Thanks again,
    Matt