Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Combining Files using Hash

by vinoth.ree (Monsignor)
on Sep 05, 2017 at 05:20 UTC ( [id://1198676]=note: print w/replies, xml ) Need Help??


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...

Replies are listed 'Best First'.
Re^2: Combining Files using Hash
by chaney123 (Acolyte) on Sep 05, 2017 at 07:07 UTC
    Hi,

    I tried this and "Use of uninitialized value $key in exists at reformat_rpt.pl" and "Use of uninitialized value $key1 in exists at reformat__rpt.pl" are shown. Why does this happen?

    Thanks
      Why does this happen?

      Maybe you have some blank lines at the end of the file. Add a check like this

      while (my $line1 = <$file2>){ next unless ($line1 =~ /\S/); # skip empty lines chomp $line1;
      poj

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1198676]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-25 14:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found