Thanks for input. Basically hash will group keys with duplicate values. I replaced with new code by commenting-out warn message with duplicate keys. While in execution, there is error "Global symbol "$hash" requires explicit package name at ./file_name.pl line 69." I don't have hash_ref to hash, to my understanding, without defining hash_ref we can't refer to/and have data written in hash table. Please correct.
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; push @{ $hash->{$key_CSV} }, $_ foreach @array_CSV; ++++ Error + ++++ print Data::Dumper->Dump([ \@array_CSV, $hash], ["array_CSV", +"hash"] ); ++++ Error +++++ #if ($hash{$key_CSV}) #{ # warn "Duplicate key '$key_CSV'"; #}; #$hash{$key_CSV} = \@array_CSV; } # Explicit scalar context my $size = scalar keys %hash; # Open the output file and save hash in $outfile_RX_CSV #open (my $outfile2, '>', "$outfile_CSV") or die "Unable to open $ +outfile_CSV: $!\n"; #print $outfile2 Dumper(\%hash); #close $outfile2; #print "Stored $size list of pins in $outfile_CSV file.\n"; # Return a reference to the hash. #return %hash; }
In reply to Re^6: csv to hash table
by waytoperl
in thread csv to hash table
by waytoperl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |