Hello Master's

Stuck at a particular logic to count total number of occurrence of a string stored in hash values. Then LOOP to print few lines of statement, also replace corresponding hash key in print statement. I don't know how to print hash keys in print statement.

All the above need to print to a file, that's opened before LOOP

Any ideas will be helpful. Thank you.

#!/usr/bin/perl use strict; use warnings; use diagnostics; use Data::Dumper; # Define Files and assign strings my $infile = "Name.template"; my $outfile = "Name.lib"; my $start = "LIB_HEADER_START"; my $end = "LIB_HEADER_END"; my $rx_start = "RX_START"; my $rx_end = "RX_END"; my $infile_CSV = "Namepinlist.csv"; my $outfile_CSV = "Namepinlist.txt"; my $DIN_CAP = "0.0005"; my $DIN_PWR_ARC = "0.000000,0.000000,0.000000,0.000000,0.000000,0.0000 +00,0.000000,0.000000"; sub main { # Open the input file open (my $infile1, '<', "$infile") or die "Unable to open $infile: + $!\n"; # Open the output file open (my $outfile1, '>', "$outfile") or die "Unable to open $outfi +le: $!\n"; # Read each line of infile1 one by one until reaching 'LIB_HEADER_ +START' while(<$infile1>) { # Exit loop when 'LIB_HEADER_START' is found at the beginning +of the line last if ($_ =~ m{\A $start }xms); } # Print each line of outfile1 until finding 'LIB_HEADER_END' to ou +tfile while(<$infile1>) { # Exit loop when 'LIB_HEADER_END' is found at the beginning of + the line last if ($_ =~ m{\A $end }xms); print $outfile1 $_; } # Read each line of infile1 one by one until reaching 'LIB_HEADER_ +START' while(<$infile1>) { # Exit loop when 'LIB_HEADER_START' is found at the beginning +of the line last if ($_ =~ m{\A $rx_start }xms); } # Print each line of outfile1 until finding 'LIB_HEADER_END' to ou +tfile while(<$infile1>) { # Exit loop when 'LIB_HEADER_END' is found at the beginning of + the line last if ($_ =~ m{\A $rx_end }xms); print $outfile1 $_; } } sub mainCSV { # Open the CSV input file open (my $infile_CSV1, '<', "$infile_CSV") or die "Unable to open +$infile_CSV: $!\n"; my %hash = (); my $hash_ref = {}; while (my $line = <$infile_CSV1>) { chomp; $line =~ s/\s*\z//; my @array_CSV = split /,/, $line; my $key_CSV = shift @array_CSV; if ($hash{$key_CSV}) { warn "Duplicate key '$key_CSV'"; }; $hash{$key_CSV} = \@array_CSV; } # Explicit scalar context my $size = scalar keys %hash; # Prints Number of Pins (Hash size...) print "Number of Pins: $size\n"; # Open the output file and save hash in $outfile_RX_CSV open (my $outfile2, '>', "$outfile_CSV") or die "Unable to open $o +utfile_CSV: $!\n"; print $outfile2 Dumper(\%hash); close $outfile2; print "Stored $size list of pins in $outfile_CSV file.\n"; # Continue print with corresponding hash key and value for AsyncIn +NoTimingArc my $count = grep { $_ eq 'AsyncInNoTimingArc' } values %hash; open (my $outfile1, '>>', "$outfile") or die "Unable to open $outf +ile: $!\n"; for (; $count >= 0; $count--) { print <<" EOF"; pin(\"$hash{$key_CSV} => {$values}\") { direction : input ; capacitance : $DIN_CAP ; } internal_power(pwr_arc){ values(\"$DIN_PWR_ARC\"); related_input : \"$hash{$key_CSV} => {$values}\" ; } EOF } } main (); mainCSV ();

In reply to 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.