in reply to why i cant print reading hash table?

Hello waytoperl,

Here are some of the problems with the code shown:

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: why i cant print reading hash table?
by GrandFather (Saint) on Nov 20, 2013 at 05:06 UTC

    Why with the C for loop again? There are, in my opinion, two better options: a while loop, or a Perl for loop. Consider:

    while ($count--) {

    or

    for (1 .. $count) {

    I'd go for the for loop variant as it makes it clear we are iterating $count times and $count isn't used in the loop body so it doesn't need to be decremented.

    True laziness is hard work

      Hello GrandFather,

      In my reply I was actually addressing this warning generated by the OP:

      Useless use of private variable in void context at...

      (I should have made this explicit.) I certainly agree that either a while or a foreach loop would be preferable here. Unfortunately, neither of your suggested alternatives results in the same number of iterations as the original:

      16:05 >perl -wE "my $c = 0; for (my $n = 3; $n >= 0; $n--) { ++$c; } s +ay $c;" 4 16:05 >perl -wE "my $c = 0; my $n = 3; while ($n--) { ++$c; } say $c;" 3 16:05 >perl -wE "my $c = 0; my $n = 3; for (1 .. $n) { ++$c; } say $c; +" 3 16:05 >

      And since the loop is likely incomplete (see my comments regarding $outfile), we cannot be sure that $count won’t be used for something later on in the loop — in which case, the foreach version of the loop would provide the values in their reverse order. So the safest alternative is probably:

      while ($count-- >= 0) {

      Update 1: Except that the value of $count within this loop is always one less than its “proper” value.

      Update 2: Upon reflection, I now realise that GrandFather’s alternative loops were not intended as equivalents to the original, but as corrections to it, since it’s most likely that the OP code meant to loop for exactly $count iterations. If so, I apologise to GrandFather for the misreading.

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        Nice that you++ picked up the off by one which exactly illustrates the problem with C for loops. I don't much like the while loop solutions because they don't make the number of iterations clear, particularly in conjunction with the post decrement operator - that's just far too subtle.

        Actually, to get the count right and clear I'd:

        for (1 .. $count + 1) {

        If the values are required I'd:

        for my $value (reverse 0 .. $count) {

        which makes it very clear that the values are required in reverse order and the span of values required, but obfuscates the number of iterations somewhat.

        True laziness is hard work
Re^2: why i cant print reading hash table?
by waytoperl (Beadle) on Nov 20, 2013 at 13:10 UTC

    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

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