in reply to build geometry group

#!perl -w use strict; my $fileopen = $ARGV[0]; my $filesave = $ARGV[1]; open(OUTPUT, ">$filesave"); open(INPUT, "$fileopen"); my $line; my @riPrimGroup; my $i; while( <INPUT> ) { # Good practice to store $_ value because # subsequent operations may change it. $line = $_; # Good practice to always strip the trailing # newline from the line. chomp($line); foreach( /^PatchMesh/ ) { my @data = split(/\s+/,$line); push(@riPrimGroup, \@data); } } close INPUT; my @riGeo1; my @riGeo2; my @riGeo3; my @riGeo4; my @riGeo5; @riGeo1 = @{$riPrimGroup[0]}; @riGeo2 = @{$riPrimGroup[1]}; @riGeo3 = @{$riPrimGroup[2]}; @riGeo4 = @{$riPrimGroup[3]}; @riGeo5 = @{$riPrimGroup[4]}; print OUTPUT "Geo1: @riGeo1\n"; print OUTPUT "Geo2: @riGeo2\n"; print OUTPUT "Geo3: @riGeo3\n"; print OUTPUT "Geo4: @riGeo4\n"; print OUTPUT "Geo5: @riGeo5\n"; print OUTPUT "\n"; close OUTPUT; _BEGIN_FILE_ PatchMesh "bilinear" 123 "periodic" 321 "nonperiodic" [ ............ array ] PatchMesh "bilinear" 456 "periodic" 654 "nonperiodic" [ ............ array ] PatchMesh "bilinear" 789 "periodic" 987 "nonperiodic" [ ............ array ] PatchMesh "bilinear" 012 "periodic" 210 "nonperiodic" [ ............ array ] PatchMesh "bilinear" ABC "periodic" DEF "nonperiodic" [ ............ array ] PatchMesh "bilinear" GHJ "periodic" KLM "nonperiodic" _END_FILE_
The above code look for a 'string' match Patchmesh and build a array from that info. and what I trying to do is to build a for loop by asignment of the array. ex. this is the info I like to build with a for loop @riGeo1 = @{$riPrimGroup[0]}; @riGeo2 = @{$riPrimGroup[1]}; @riGeo3 = @{$riPrimGroup[2]}; @riGeo4 = @{$riPrimGroup[3]}; @riGeo5 = @{$riPrimGroup[4]}; so I thought about this for ($i = 0; $i < $#riPrimGroup; $i += 1 ) { @riGeo$i = @{$riPrimGroup[$i]}; } but it doesn't work, maybe a join could be used to build the for loop and append it to the arrayname 'riGeo+newname' the idea is to read in a file 'geometry data' extrate information PatchMesh or another info and build a ArrayName of how many PatchMesh it is find this way I could print information about this info and build the 'geometry' group for export to another format. regards, Fredrik Gustafsson

Replies are listed 'Best First'.
Re: Re: build geometry group
by Anonymous Monk on Feb 14, 2003 at 02:25 UTC

    Okay. So your problem is that you want to give individual names to the arrays contained within an Array of arrays. You know how to do this for a known number of arrays, but your data contains a variable number of them and you're unsure of the syntax required to do this dynamically.

    The question you've got to ask yourself, is Why?

    Reading your example code, it seems likely it's because you don't know how to refer to the elements of the nested arrays, and you think that by doing this is will be easier to refer to $riGeo5[3]?

    If this is your reasoning, I think you will find that havng done this, it will make life harder rather than easier. For example, you show the code.

    ... print OUTPUT "Geo1: @riGeo1\n"; print OUTPUT "Geo2: @riGeo2\n"; print OUTPUT "Geo3: @riGeo3\n"; print OUTPUT "Geo4: @riGeo4\n"; print OUTPUT "Geo5: @riGeo5\n"; ....

    Once you have achieved your renaming, printing out 5 arrays takes 5 print statements and if you had 1000 arrays you would need 1000 print statements!(*)

    However, by sticking with the AoA's that you have, you can acheive the 5 lines above with one line:

    print "Geo$_: @{$riPrimGroup[$_-1]}\n" for 1..5;
    and with one small modification, that line will handle 1000 arrays or any number that you can fit in memory:

    print "Geo$_: @{$rePrimGroup[$_-1]}\n" for 1..@riPrimGroup;

    Now, getting back to the syntax of accessing the individual elements of the nested arrays. If you want access the 2nd element of the 4th array, the syntax is:

    print $riPrimGroup[3][2];

    I hope that will have convinced you that you don't need to rename the nested arrays with individually numbered names, and that you will take the time to read and understand perlman:perlreftut.

    If however, you are not convinced and still want to do this, then the syntax you are searching for is referred to as "Symbolic References" and is described in a section of perlman:perlref with that name.

    I would also strongly urge you to read and understand this before you read the section referenced above. If you still want to do it after that...I wish you the best of luck :)

    However, be warned that, whilst symbolic references have their uses, they can lead to some very difficult to track down bugs. If you elect to use them without having a really good reason for doing so and you encounter problems, you are quite likely to not get a sympathetic hearing here at the Monastery.

    (*) Not strictly true or perhaps that should be: Not no strict;ly true?