in reply to How to read each line of a file into a nested array

Short answer:
001 use strict; 002 my @array; 003 open (INFILE, "<filename") or die "could not open file"; 004 foreach (<INFILE>) { 005 chomp; 006 push @array, [m/(\d+)/, $_]; 007 } 008 close INFILE;

Line by line explaination:

Line 1: rule #1 always use strict it will save you hours in the long run.

Line 2: Create a place to put our answers

Line 3: open your data file INFILE is now the file handle for your data file. Relplace "filename" with the name of your file. It is good practice to always check the results of an open open will return a false value if it fails and a true value if it suceeds the right side of the or will never happen if it suceeds but we will die if it fails.

Line 4: The diamond operator  <> reads one line from INFILE and sets the value of $_ to that string.

Line 5: chomp the newline off the end of $_

Line 6: Line noise? Let's break it down. push @array, will add whatever comes after the comma to the end of @array, "push" them on to the end. What is it pushing? Well the square brackets [] will turn whatever is inside of them into an anonymous array. This anonymous array will be push onto the end of @array. So Line 6 could be rewritten as

$_ =~ m/(\d+)/; #set $1 to the first group of digits push @array, [$1, $_];
or
$_ =~ m/(\d+)/; my @subarray = ($1, $_); push @array, [@subarray];
or even
$_ =~ m/(\d+)/; my @subarray = ($1, $_); push @array, \@subarray;
etc., etc., etc. TMTOWTDI

Line 7: End of for loop

Line 8: close the file

Anyway,
$array[0][0] will be "1"
$array[0][1] will be "1 2 3 4 5"
$array[1][0] will be "10"
$array[1][1] will be "10 20 30 40 50"
$array[2][0] will be "200"
$array[2][1] will be "200 100 300 2 1"

--

flounder

Replies are listed 'Best First'.
Re: Re: How to read each line of a file into a nested array
by opolat (Novice) on Jun 22, 2002 at 11:22 UTC
    One more quick question ! If my NASTRAN output file was something like this:

    1 2 3 4 5

    1 20 30 40 50

    1 9 3 3 6

    4 6 7 9 0

    4 4 8 90 8

    4 5 2 7 20

    9 5 2 7 20

    9 20 30 40 50

    9 4 8 90 8

    The start of each line (The first number) represent an element ID. Would it possible to create a hashes of nested arrays where each element ID is the key for the created nested array. I know it does not make sense, but I need a seperate nested array for each element ID (start of each line) so that I can sort it later on based on a chosen column and print it. I have to get the all the lines that starts with first element put them in a nested array in a way which is described in the first message and later create a hash where the key is the element ID and points to a nested array which has all the information about that particular element. Basically: Element ID (key for the hash) = 1 points to a nested array of

    1 '1 2 3 4 5'

    1 '1 20 30 40 50'

    1 '1 9 3 3 6'

    Element ID = (key for the hash) = 4 points to a nested array

    4 '4 6 7 9 0'

    4 '4 4 8 90 8'

    4 '4 5 2 7 20'

    Element ID = (key for the hash) = 9 points to a nested array

    9 '9 5 2 7 20'

    9 '9 20 30 40 50'

    9 '9 4 8 90 8'

    I am not sure if this is the best way of doing it but I can not think of any other way. Any Help will be appreciated. Thanks a lot.

Re: Re: How to read each line of a file into a nested array
by Anonymous Monk on Jun 22, 2002 at 08:30 UTC
    Thanks flounder99, it works!, and thanks for the detailed explanation!