in reply to Re: build geometry group
in thread build geometry group
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:
and with one small modification, that line will handle 1000 arrays or any number that you can fit in memory:print "Geo$_: @{$riPrimGroup[$_-1]}\n" for 1..5;
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?
|
|---|