http://qs1969.pair.com?node_id=1037159


in reply to Constructing hashes

Aside from a few harmless, but unlogical parts of your programs (for example, why do you chomp $_ when you already know that it can't end with a newline), I don't find that you clearly expressed your problem, so I have to guess a bit:

The listing you write under "Example" is what should end up in the logfile, right? In this case, you just need to remember, whether you have already printed the server name for the logfile. Each server causes in several lines being printed (one line for each component), but only the first line will also contain the server name. I would therefore write your print statement in the following way:

print "$server_name_or_blank $RM\n";
and make sure that on the first iteration of the inner loop, $server_name_or_blank contains the name of the server, and on subsequent iterations of the inner loop it contains a string of spaces, of the same length the server name had been.

A few additional remarks: First, always have "use strict; use warnings;" in your program. Second, I strongly advice against using $_ as a variable for the outer loop. There are many cases where Perl operations implicitly modify $_. Finally, don't modify the loop variable, unless you really know what you are doing; the loop variable is an alias to the respective list element, and by changing the variable, you also change the list element. Though this is harmless in your particular case, you will be bitten sooner or later, if you continue this practice.
-- 
Ronald Fischer <ynnor@mm.st>