Hi waytoperl. I think I understand what you are trying to achieve. If not, there should be enough code here for you to modify as needed.
Input CSV
PinName1,Group1 PinName2,Group1 PinName3,AsyncInNoTimingArc PinName4,Group1 PinName5,AsyncInNoTimingArc PinName6,AsyncInNoTimingArc PinName5,AsyncInNoTimingArc
Output RX CSV
$VAR1 = { 'PinName1' => [ 'Group1' ], 'PinName3' => [ 'AsyncInNoTimingArc' ], 'PinName6' => [ 'AsyncInNoTimingArc' ], 'PinName5' => [ 'AsyncInNoTimingArc' ], 'PinName2' => [ 'Group1' ], 'PinName4' => [ 'Group1' ] };
Output CSV
pin(PinName3) { direction : input ; capacitance : 0.0005 ; } internal_power(pwr_arc){ values("0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000 +,0.000000"); related_input : "PinName3" ; pin(PinName5) { direction : input ; capacitance : 0.0005 ; } internal_power(pwr_arc){ values("0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000 +,0.000000"); related_input : "PinName5" ; pin(PinName6) { direction : input ; capacitance : 0.0005 ; } internal_power(pwr_arc){ values("0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000 +,0.000000"); related_input : "PinName6" ; ** Found 3 occurrences of 'AsyncInNoTimingArc' **
Script
#!/usr/bin/perl use strict; use warnings; use diagnostics; use Data::Dumper; die "Error. Usage \'perl csv_parse.pl inputfile.csv outputfile_RX.csv +outputfile.csv\'\n $!" unless $#ARGV == 2; my $infile_CSV = shift @ARGV; my $outfile_RX_CSV = shift @ARGV; my $outfile_CSV = shift @ARGV; sub mainCSV ($$$); # Assign Global Strings 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 mainCSV ($$$) { my $infile_CSV = shift; my $outfile_RX_CSV = shift; my $outfile_CSV = shift; my %hash = (); my $hash_ref = {}; # Read the CSV input file open (my $infile_CSV1, '<', "$infile_CSV") or die "Unable to open +$infile_CSV: $!\n"; while (<$infile_CSV1>) { chomp; ## Skip blank line next if ($_ eq ""); $_ =~ s/\s*\z//; my @array_CSV = split(/,/,$_); my $key_CSV = shift @array_CSV; if (exists $hash{$key_CSV}) { warn "Duplicate key: $key_CSV\n"; } else { $hash{$key_CSV} = \@array_CSV; } } close($infile_CSV1); # Explicit scalar context my $size = scalar keys %hash; # Prints Number of Pins (Hash size...) print "Number of Pins: $size\n"; # Open output CSV in Write Mode. The output file and save hash in +$outfile_RX_CSV open (my $outfile1, '>', "$outfile_RX_CSV") or die "Unable to open + $outfile_CSV: $!\n"; print $outfile1 Dumper(\%hash); close($outfile1); # Open output CSV File in Append Mode open (my $outfile2, '>>', "$outfile_CSV") or die "Unable to open $ +outfile_CSV: $!\n"; print "Stored $size list of pins in $outfile_CSV file.\n"; + # Print key,value where the hash value is AsyncInNoTimingArc my $count_occurrences = 0; foreach my $key (sort keys %hash) { ## Does current hash key value equal AsyncInNoTimingArc if ( @{$hash{$key}}[0] eq "AsyncInNoTimingArc" ) { $count_occurrences++; print $outfile2 "\n", "pin($key) {\n", "direction : input ;\n", "capacitance : $DIN_CAP ;\n", "}\n", "\n", "internal_power(pwr_arc){\n", "values(\"$DIN_PWR_ARC\");\n", "related_input : \"$key\" ;\n"; } } print "\n** Found $count_occurrences occurrences of 'AsyncInNo +TimingArc' **\n"; print $outfile2 "\n\n** Found $count_occurrences occurrences o +f 'AsyncInNoTimingArc' **\n"; close($outfile2); } mainCSV ($infile_CSV,$outfile_RX_CSV,$outfile_CSV);
In reply to Re^3: why i cant print reading hash table?
by VincentK
in thread why i cant print reading hash table?
by waytoperl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |