http://qs321.pair.com?node_id=1063600


in reply to Print Hash Keys to a file for corresponding Hash values

I don't understand your description of your problem or what you want to achieve, so I am guessing here...

I guess you are having difficulty with the output to $outfile at the end of sub mainCSV and that you want "pin" and "related_input : " to be followed by the key and corresponding values from your hash.

If my guess is correct, there are a couple of problems:

The values of %hash are references to arrays, so I don't think the grep just after "Continue print with corresponding hash key and value for AsyncIn" does what you want.

Then, iterating a loop for the number of times there is a particular value in the hash doesn't make it easy to access the keys or values of the hash.

So, you need a different approach, and it is not clear from what you have presented exactly what you are trying to achieve.

Here is something for you to consider but, again, it is only a guess as to what you want and it is untested:

# Continue print with corresponding hash key and value for AsyncIn +NoTimingArc # Find the keys for which AsyncInNoTimingArc is in the value array my @keys = grep { my @array_CSV = @{$hash{$_}}; grep { $_ eq 'AsyncInNoTimingArc'} @array_CSV; } keys %hash; open (my $outfile1, '>>', "$outfile") or die "Unable to open $outf +ile: $!\n"; foreach my $key (@keys) { # Get the values corresponding to this key my $values = join(', ', @{$hash{$key}}); print <<" EOF"; pin(\"$key => {$values}\") { direction : input ; capacitance : $DIN_CAP ; } internal_power(pwr_arc){ values(\"$DIN_PWR_ARC\"); related_input : \"$key => {$values}\" ; } EOF }