in reply to How can I merge data using a hash?

I'm assuming every line contains data and that the files are in tab delimited format.
use strict; my @file1 = ( "complex.1\t1.356\n", "complex.2\t1.879\n", ); my @file2 = ( "12.02\t/home/complex.2out\n", "10.25\t/home/complex.1out\n", ); my (%hash, $ref, $values); chomp(@file1); chomp(@file2); foreach (@file1) { $_ =~ /^complex\.(\d+)\t([\d\.]+)$/; $hash{$1}->[0] = $2; } foreach (@file2) { $_ =~ /^([\d\.]+)\t\/home\/complex.(\d+)out$/; $hash{$2}->[1] = $1; } foreach (sort {$a <=> $b} keys %hash) { $ref = $hash{$_}; $values = join("\t", @$ref); print "complex.$_\t$values\n"; }

Replies are listed 'Best First'.
Re^2: How can I merge data using a hash?
by Anonymous Monk on Oct 10, 2004 at 23:00 UTC
    could you explain this:
    $hash{$1}->[0] = $2
    is it a reference to an array? so the key is $1...
      Yes, it's a reference to an array. For instance:
      $hash{$1}->[0] = $2; $ref = $hash{$1}; print @$ref[0];
      $ref points to the array. @$ref is the array that $ref points to.