in reply to Re: read hash table from a subroutine or retrieve previously stored hash table from a file
in thread read hash table from a subroutine or retrieve previously stored hash table from a file

Moving forward, I tried to initially just print hash table using hashref. Please help me try to understand. I know somehow I'm referring to array address, instead of actual value stored in memory.

Expected Output:

C_WREN => Group1 CAL_CLK => Group1 RX_IBIAS_2_25U[4:0] => Group3 REF_RATE[9:0] => Group3 AVD0 => Group4 SIDDQ => Group7

Main Program

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 = \%hash; 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"; # Method 1 to print hashref table while( my ($key, $value) = each (%$hash_ref )) { print "$key => $value\n"; } }

Actual output:

C_WREN => ARRAY(0x13b2eab0) CAL_CLK => ARRAY(0x13ad9ea0) RX_IBIAS_2_25U[4:0] => ARRAY(0x13b2e6a0) REF_RATE[9:0] => ARRAY(0x13b0e090) AVD0 => ARRAY(0x13a2f200) SIDDQ => ARRAY(0x13b0dd00)
  • Comment on Re^2: read hash table from a subroutine or retrieve previously stored hash table from a file
  • Select or Download Code

Replies are listed 'Best First'.
Re^3: read hash table from a subroutine or retrieve previously stored hash table from a file
by marinersk (Priest) on Nov 20, 2013 at 00:20 UTC
    This line:

     $hash{$key_CSV} = \@array_CSV;

    Is causing your most immediate problem. You are storing the reference to the array in your hash elements. So when you print out the hash elements, you are getting a reference to an array.

      well, im saving array_cvs contents in hash table. What's is the other way of representation referring value. Please let know.

        Your expected output does not show arrays of values. It shows single scalar values. So your question does not make sense to me. I must be missing something.

        Let me analyze how the code aligns with your stated goals in more detail -- on the surface it simply seemed like you wanted scalar but were storing arrays.