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

Hi, I would like to use a 3-D array for storing input from a file. I would like to structure it like so. Block[0][0][0] I want to have a list of blocks, each block has an list of vertex in that block, and each vertex has three points. My problem is that within each block there is a arbitrary number of vertex, so I don't know how to structure it when there is no way to know how many vertex are in the block until you have read it in, and each block has a different number of vertex. Any help on this would be great. Thanks. Stuart

Replies are listed 'Best First'.
Re: using a 3D array
by davorg (Chancellor) on Feb 07, 2003 at 14:37 UTC

    Perl has no problem dealing with situations like this as arrays in Perl are not fixed in size, so you can just grow them to whatever size is necessary whilst you are populating them.

    It's hard to be any more help without seeing the format of your input data.

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

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      davorg, Thanks for the reply. I am sending you an example of the data that I am looking to read from a file into the array. WHat I am looking for is a list of blocks, each block has a list of vertices, and each vertex has 4 values. I am also looking for a way to "check" off blocks once I have written them, and the same for the vertices. Anyway, I am really looking for a structure of a list of blocks, which contain a list of vertices (each block has a different number of vertices) and then each vertex contain four seperate values. Thanks for your help.
      1 455.462708 1026.615005 2 428.309624 1073.409507 3 730.386784 1248.693485 4 757.539868 1201.898982 5 455.462708 1026.615005 88888 6 367.834421 1038.319919 7 340.681337 1085.114421 8 642.758497 1260.398399 9 669.911581 1213.603896 10 367.834421 1038.319919 88888 11 260.028495 1812.352286 12 294.596155 1793.896502
Re: using a 3D array
by tall_man (Parson) on Feb 07, 2003 at 15:45 UTC
    Multidimensional arrays in perl are actually arrays of references. Here's how it really works:

    Suppose we have $Block[0][0][0]. That's short for $Block[0]->[0]->[0]

    @Block is an array.
    $Block[0] contains a reference to another array.
    $Block[0]->[0] gets you to the first element in that reference, which is another reference to an array.
    $Block[0]->[0]->[0] gets to the first element in that inner reference.

    For details, see the perlref documentation. The bottom line is that each level can grow independently, so there is no problem with ragged array dimensions.

Re: using a 3D array
by artist (Parson) on Feb 07, 2003 at 17:31 UTC
    If you like to use object oriented features, you can make your task easier.
    #!/usr/bin/perl -w package Vertex; sub new { my $class = shift; my $self = [@_]; bless $self, $class; } package Block; sub new { my $class = shift; my $self = [@_]; bless $self, $class; } package main; use strict; # @blockList has 2 blocks b1 and b2 # Block b1 has 2 vertexes v1 and v2 # Block b2 has 3 vertexes v1, v4 and v3 # Vertex v1 has 3 points (2,3,4) # Vertex v2 has 3 points (5,6,7) .... my @blockList; my $v1 = new Vertex(2,3,4); my $v2 = new Vertex(5,6,7); my $v3 = new Vertex(8,9,10); my $v4 = new Vertex(11,12,15); my $b1 = new Block($v1,$v2); my $b2 = new Block($v1,$v4,$v3); push @blockList,($b1,$b2);
    Just a curiosity... Is this for a game design or graph theory?. There might be something more available on the CPAN for your purpose.

    artist