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 }

In reply to Re: Print Hash Keys to a file for corresponding Hash values by ig
in thread Print Hash Keys to a file for corresponding Hash values by waytoperl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.