user1987607
user1987607

Reputation: 2157

merging duplicate values and copying between tables

This is how my situation looks like. I have two tables:

Table 1

C0   C1   C2   C3    ...    C8
     1                      10
     2                      50
     3                      70
     3                      20

Table 2

C0   C1   C2   C3    ...    C7
     1                      
     2                      
     3                      

Table 2 should become like this

C0   C1   C2   C3    ...    C7
     1                      x = 10
     2                      x = 50
     3                      x = 70,20

So basicly what I want to do is compare the values of C1 of both tables. If the values are the same then the values of C8 (table 1) should be copied to C7 (table 2) and "x =" should be added. However when there is a duplicate value in C1 (table1), like here the 3, then in table 2 these values should be put next to each other, seperated by a ',' (like here x = 70,20)

This is what I have so far

my $table1 = $ARGV[0];
my $table2 = $ARGV[1];

# Open my file (I use exactly the same code for opening table 2)
unless ($table1) {
    print "Enter filename of file:\n";
    $table1 = <STDIN>;
    chomp $table1;
    }
open(DATA1,'<',$table1) or die "Could not open file $filename $!";

# Here I push the values of C8 in table 1 to the same row
my %info = ()    
while (<DATA1>) {
    my @columns = split;
if( exists $info{ $columns[1] } ) {
    push @{ $info{ $columns[1] }->{var} }, $columns[8];
}
else {
    $info{ $columns[1] } = { var =>[ $columns[8] ] }
}
}   

If this is all correct, the only thing I need to do now is to copy the values to C7 (table 2) and let them start with "x="

Could someone help me with this?

Upvotes: 0

Views: 98

Answers (2)

Vorsprung
Vorsprung

Reputation: 34357

I think this is what you are asking for. See perldoc perldsc and perldoc perllol for details on how the access to the @line in %t1 works

use strict;
use warnings;

my $table1 = $ARGV[0];
my $table2 = $ARGV[1];

open(my $fh1,$table1) || die "$! $table1";
open(my $fh2,$table2) || die "$! $table1";

#discard the first lines which are the headers
my $headers1=<$fh1>;
my $headers2=<$fh2>;

#set up data from table1
my %t1=();
while(<$fh1>) {
    chomp;  #remove newlines
    my @line=split(/\t/);  #split on tabs
    my $keyfield=$line[1]; #key is C1
    push @{$t1{$keyfield}}, $line[8];
}

#read from table2 and write output
while(<$fh2>) {
    chomp; #remove newlines
    my @line=split(/\t/);  #split on tabs
    my $keyfield=$line[1]; #key is C1
    my @x=();
    if (exists($t1{$keyfield}) ) {
        push @x,@{$t1{$keyfield}}; # get the C8 values from t1
    }
    for my $c (0..6) {
        print $line[$c],"\t"; #print C0 to C6 with a tab seperator
    }
    print "x = ",join(",",@x),"\n";    #do x=70,20 
}

Upvotes: 1

Krishnachandra Sharma
Krishnachandra Sharma

Reputation: 1342

You are trying to map an array reference into a string!

What you can do is,

  1. Copy the array's data in a $string.
  2. Concatenate $string as $string = "x = $string";.
  3. Assign this modified $string to the hash(%info) in place of array reference. Sice both are scalars, assignment shouldn't be a problem.

Upvotes: 0

Related Questions