http://qs321.pair.com?node_id=1198676


in reply to Combining Files using Hash

Hi

pls use,

use strict; use warnings; for better error.

$value3 are not initialized. Anyone know how to fix this?

my($key,$value1,$value2,$value3) = $line =~ /(\w+),(\d+.\d+),(.\d+\s+\ +d+.\d+.)/g;

In regular expression you have only three group and assigning into four variables, so $value3 will be undef.

Update:

Here its the fixed code

use strict; use warnings; my %file1Hash; my $value4; open my $file1, "<","file1.csv" or die $!; open my $file2, "<","file2.csv"or die $!; open my $outfile_1, ">", "combined.rpt.csv" or die $!; while( my $line = <$file1>){ chomp $line; my($key,$value1) = (split /,/, $line); $file1Hash{$key} = $value1; } while(my $line1 = <$file2>){ chomp $line1; my($key1) = (split /,/, $line1); if (exists $file1Hash{$key1}) { print $line1.",".$file1Hash{$key1}."\n"; } else { # print $line1."\n"; } } close $file1; close $file2; close $outfile_1; exit 0;

All is well. I learn by answering your questions...