Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Print Hash Keys to a file for corresponding Hash values

by waytoperl (Beadle)
on Nov 20, 2013 at 17:01 UTC ( [id://1063558]=perlquestion: print w/replies, xml ) Need Help??

waytoperl has asked for the wisdom of the Perl Monks concerning the following question:

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 ();

Replies are listed 'Best First'.
Re: Print Hash Keys to a file for corresponding Hash values
by ig (Vicar) on Nov 20, 2013 at 20:28 UTC

    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 }
Re: Print Hash Keys to a file for corresponding Hash values
by VincentK (Beadle) on Nov 20, 2013 at 20:05 UTC

    Hi waytoperl.

    A google search yielded this page.

    http://stackoverflow.com/questions/6309821/how-do-i-find-and-count-duplicate-values-in-a-perl-hash

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1063558]
Approved by Laurent_R
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-03-28 15:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found