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

Hi, and thanks to all reply of my previus 'issue'

This one is about asign data to a group.

The code I have only works with 1 geometry

ex.

begin PatchMesh ................... Info "P" [ Data Array ... ] end
------------------------------

So I'm trying to build a group of geometry data.

foreach object build a group.

ex.

__DATA__ PatchMesh "bilinear" 123 "periodic" 321 "nonperiodic" "P" [ 1 2 3 4 5 6 ... ]
do paste the above info to a groupname
next do: PatchMesh "bilinear" 123 "periodic" 321 "nonperiodic" ... next do: PatchMesh "bilinear" 123 "periodic" 321 "nonperiodic" ... end of line.
I have code to build the first line and array but need some suggestion to build 'GroupArray' there I have something like
@GeometryGroup = { patchmesh1, patchmesh2 ...... }
I thought about if I have all the code to export 1 geometry then if I asign this info into a array again 'grouparray'

if more code need to understand this problem or my message about 'build geometry data...' is the same project I'm trying to build with Perl.

regards, Fredrik Gustafsson

update (broquaint): removed <pre> tags and added formatting

Replies are listed 'Best First'.
Re: build geometry group
by BrowserUk (Patriarch) on Feb 13, 2003 at 15:11 UTC

    Hi Fredrik. I'm afraid that your question is a little confusing. If you could re-word it, avoiding using terms (like GroupArray:) which will only make sense in terms of your project, but mean nothing to most of the rest of us, that would help.

    It's not at all clear (to me anyway) exactly what the question is that you are asking? I would suggest that you re-write your question and post it as a reply to your original.

    One method I use to clarify my own questions is to try and explain the problem to a non-technical person first--your spouse, a security man, the dog:)--anyone not involved in the project would do as that forces you to word the question in terms they understand. You need to give us (at least) the input to the problem, the required output, and some form of explanation of how to get from the first to the second. This is often best done by showing us the code you have, the results it is currently producing and how this differs from what you need.

    That type of question nearly always gets several answers here.

    Oh, and when you post again, could you please use <code>....your program goes here....</code> tags instead of <pre>...</pre> tags. This will save the editors here some work. Thanks.


    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: build geometry group
by Frippe (Acolyte) on Feb 13, 2003 at 17:38 UTC
    #!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

      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?