in reply to Re: why i cant print reading hash table?
in thread why i cant print reading hash table?

Thank you Athanasius for pointing out problems in code. Made recommended modifications.

Declared variables

$infile_CSV, $outfile_CSV, $outfile

Using 'Asyn...Arc' removes Bareword error

Using for (; $count >= 0; $count--)

Placed $outfile outside Loop.

The print is modified with EOF These introduce some syntax errors. <<EOF prints strings available till EOF. In my code there are syntax problems.

Most of the errors are removed. There are errors related to $key_CSV. Please take a look. Earlier logic was to search 'AsyncInNoTimingArc' from %hash values. Keep a count of occurrence in $count. Using $count in LOOP print a bunch of line by adding every corresponding %hash table $key_CSV for every occurrence of 'AsyncInNoTimingArc'. Please provide your insight logically its possible, where we can search a hash value, keep a count and in every LOOP iteration of value[] key[] use print statement and use content of key[]

EXAMPLE: Output of %hash have its respective $key_CSV and values. Search a %hash value, keep a $count and for every values print respective $key_CSV in print statement

Contents of %hash{}

$VAR1 = { 'REFCLK_BUF3' => [ 'AsyncOutNoTimingArc' ], 'C_WREN' => [ 'C_CLK_and_SCANCLK_L__IN' ], 'CAL_CLK' => [ 'AsyncInNoTimingArc' ], 'RX_IBIAS_2_25U[4:0]' => [ 'Power' ],

open (my $outfile1, '>>', "$outfile") or die "Unable to open $outfile: + $!\n"; for (; $count >= 0; $count--) { print <<" EOF"; pin($key_CSV) { direction : input ; capacitance : $DIN_CAP ; } internal_power(pwr_arc){ values(\"$DIN_PWR_ARC\"); related_input : \"$key_CSV\" ; } EOF

Replies are listed 'Best First'.
Re^3: why i cant print reading hash table?
by VincentK (Beadle) on Nov 20, 2013 at 17:34 UTC

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