She-wolf has asked for the wisdom of the Perl Monks concerning the following question:

I need to read files(or a file) into nested arrays.
Specifically, I need to do a schedule which has 12 elements per project, and then up to 100 projects per group. I need to input the information from a file for the whole group or individual files for each project.

I want to put project info into one array and then those arrays into a larger array for the whole group.
Please help!

She-wolf
"Wha? I don't get it."

Replies are listed 'Best First'.
Re: Inputing info into Nested Arrays
by Cirollo (Friar) on Aug 21, 2000 at 20:21 UTC
    If you want to create a multidimensional array, you do it like this:
    @array = ( [ "foo", "bar"], [ "abc", "def"] );
    You can access elements with 2 array subscripts - $array[0][0] is "foo", $array[0][1] is "bar", $array[1][0] is "abc", etc.

    Supposing that your file of projects had each project on 1 line, you could do this:

    while ( <FILE> ) { push @array, [ split ]; }

    Then to print it back out,

    for $i (0..$#array) { for $j (0..$#{$array[$i]}) { print "Element $j of project $i is $array[$i][$j]\n"; } }

    See The Perl Data Structures Cookbook for more info.

      The whole:
      @array = ( [ "foo", "bar"], [ "abc", "def"] );

      thing seems to work just for small stuff, is there another way that will be a little easier for a large number of references?

      Also, I don't see how the while ... bit will work.(please excuse me, I'm a newbie)

      She-wolf
      "Wha? I don't get it."

        The while... bit splits each line up and puts the resulting array into an element of @array.

        split with no arguments takes a line from $_ and splits it on whitespace (e.g, /\s+/) after disregarding leading spaces. So, that creates the 'inner' array from each line (each word in the line is 1 element), and then the 'outer' array is created with each line as an element. So,

        This is a test. This is line #2
        would be read in like this:
        $array[0][0] = "This"; $array[0][1] = "is"; $array[0][2] = "a"; $array[0][3] = "test"; $array[1][0] = "This"; $array[1][1] = "is"; $array[1][2] = "line"; $array[1][3] = "#2";

        And of course, you are going to have to change this based on your data file's structure.

Re: Inputing info into Nested Arrays
by merlyn (Sage) on Aug 21, 2000 at 20:05 UTC
      I want to try to do this without having to install any modules additional modules(right now I have CGI.pm and Sprite.pm) Is there any way to do this without getting a module?

      She-wolf
      "Wha? I don't get it."

        You can almost certainly do it without a module. If you were to give us a brief sample of the input data and a description of the data structure that you wanted then I'm sure that someone could help you out.

        Alternatively, by reading perldoc perllol and perldoc perldsc you may well be able to work it our for yourself.

        --
        <http://www.dave.org.uk>

        European Perl Conference - Sept 22/24 2000, ICA, London
        <http://www.yapc.org/Europe/>
RE: Inputing info into Nested Arrays
by xdb19 (Sexton) on Aug 21, 2000 at 21:27 UTC
    I do not think of nested arrays in the same way as you do. You think encapsulation of arrays, whereas I thing 3 dimensional arrays. A cube for example, would serve you just fine. The first field would be the group, the second - the project, and the third (i'm guessing) the months. Using a hash is a better idea, because it allows instant access to the data once you know the keys. the code to parse through the following text HASH <xmp> __DATA__ GROUP1 PROJECT1 DECEMBER DATA GROUP2 PROJECT2 JANUARY DATA GROUP1 PROJECT6 JULY DATA GROUP4 PROJECT9 MAY DATA GROUP5 PROJECT5 AUGUST NOW __END DATA__ </xmp> is as follows:
    while ( <DATA> ) { chomp( $_ ); #get rid of whitespace at end @f = split( /\s/, $_ ); #split data up into an array #ThreeDHash[ GROUP ][ PROJ ][ MONTH ] = DATA $ThreeDHash{ $f[0] }{ $f[1] }{ $f[2] } = $f[ 4 ]; }
    For Arrays, you can put things into a 3d Array as follows
    #same as a hash, only with square brackets # dont forget $index1/2/3 all have to be integers # so you will have to index every entry to the file. $ThreeDArray[ $index1 ][ $index2 ][ $index3 ] = $DATA;
    Have Fun. Regards, XDB19
RE: Inputing info into Nested Arrays
by princepawn (Parson) on Aug 21, 2000 at 21:27 UTC
    I would suggest you look at the CPAN module Text::TreeFile which allows for the creation of hierarchical data structures directly from files.

    Also, you might take a look at Lincoln Stein's Boulder and Stone classes (author ID: LDS) which do the same thing.

    On second thought, look at LDS' stuff first as he is one of the most prominent Perl coders and everything he writes is widely used and tested... I'd even recommend him over anything I wrote myself! princepawn ... but Tye calls me "PrincePawn" <smile>