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

I want to thank everyone for helping me, but I am still having trouble. I am trying to get data from a file, like the one below. The 88888 marker means the end of a block. What I am trying to do is have a list of blocks which contain a list of vertices, which contains four elements. I am a little confused on how to read the vertices into the array when I don't know how many there are until I read the file. I apologize if I am repeating myself, but I am still confused. I am using a two dimensional array now and below the data is the code I use to get that. Could I do something like $Block[0] = @datavertex[0];? SO that I can have $Block[0][0][1]
1 786.582394 1480.624270 2 877.141985 1284.635536 3 779.147607 1239.355735 4 688.588015 1435.344469 5 786.582394 1480.624270 88888 6 664.833374 241.139500 7 661.222659 245.226628
VERTEX: while ($line1 = <FHV>) { if ($line1 == $ek) { # print "BLOCK END\n"; $line1 = <FHV>; if ($line1 = eof) { last VERTEX; } } my @vertex = ( scalar($line1), scalar(<FHV>), scalar(<FHV>), scalar(0) ); $vertexcount = $vertexcount + 1; chomp @vertex; push @datavertex, \ @vertex; }

Replies are listed 'Best First'.
Re: 3D arrays, again
by cfreak (Chaplain) on Feb 07, 2003 at 23:07 UTC

    I'm confused by your 88888 to read blocks (maybe because I'm not seeing it in your data), as far as creating multi-diminsional arrays, they aren't really supported in Perl but you can duplicate the functionality using references.

    Could I do something like $Block[0] = @datavertex[0];? SO that I can have $Block[0][0][1]

    Not really, use references. Something like this would do it:

    my @array = qw(oranges bannanas apples kiwi); my @array2 = (); $array2[0] = \@array; my @array3 = (); $array3[0] = \@array2; # ad nausium ...

    So now you could dereference and get 'oranges' from the third array my $fruit = $array3[0]->[0]->[0];

    Also if you're using a loop to set up that same structure you can do it in one statement, like this (assuming you already defined array3:

    $array3[0] = [['oranges','bannanas','apples','kiwi']]; # and multiple ones like this $array3[0] = [['oranges','bannanas'],['apples','kiwi']];

    I hope that gives you a better idea, I'm not understanding exactly what it is your doing, maybe if you provide some of the data I could give you a better example.

    Chris

    Lobster Aliens Are attacking the world!
Re: 3D arrays, again
by BrowserUk (Patriarch) on Feb 07, 2003 at 23:55 UTC

    I think this gets close to what your looking for

    #! perl -slw use strict; my (@blocks, $datavertex); # Build the structure while(my $in = <DATA>) { if ( $in =~ /^88888/ ) { push @blocks, $datavertex; $datavertex = []; next; } my @vertex = ($in, scalar <DATA>, scalar <DATA>, 0); chomp @vertex; push @{$datavertex}, \@vertex; } push @blocks, $datavertex if @$datavertex; #! push the last block # Accessing it for my $block (0..$#blocks) { for my $dv ( 0 .. $#{$blocks[$block]} ) { for my $vertex ( 0 .. $#{$blocks[$block][$dv]} ) { print"[$block][$dv][$vertex] =>", $blocks[$block][$dv][$ve +rtex]; } print ''; } } __DATA__ 1 786.582394 1480.624270 2 877.141985 1284.635536 3 779.147607 1239.355735 4 688.588015 1435.344469 5 786.582394 1480.624270 88888 6 664.833374 241.139500 7 661.222659 245.226628

    The output


    Examine what is said, not who speaks.

    The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

Re: 3D arrays, again
by justinNEE (Monk) on Feb 07, 2003 at 23:52 UTC
    stu, is this what you want? $array2[0][0] is 'oranges'.
    my @array = qw(oranges bannanas apples kiwi); my @array2 = (); $array2[0] = \@array; # ad nausium ... print $array2[0][0];
    update forgot to address the other part of your question. "I am a little confused on how to read the vertices into the array when I don't know how many there are until I read the file." You are doing this by saying, while there is another line of data to be read, ...
    while ($line1 = <FHV>) {