in reply to match two files

Although overkill for the example you showed, it might be useful to see how this can be done with SQL. The following uses DBD::CSV so it can work with your input files directly to produce your expected output, though for "production" work you would probably want to use a real database. Also, my output code is somewhat simplistic, one could use Text::CSV(_XS) for that purpose as well.

use warnings; use strict; use DBI; my $dbh = DBI->connect("dbi:CSV:", undef, undef, { csv_sep_char => "\t", f_ext => '', RaiseError => 1, }) or die "Cannot connect: $DBI::errstr"; my $sth = $dbh->prepare(<<'ENDSQL'); SELECT tmp01.PeptideID as PeptideID, tmp01.ProteinID as ProteinID, tmp02.SpectrumID as SpectrumID, tmp02.Sequence as Sequence FROM tmp01 LEFT OUTER JOIN tmp02 ON tmp01.PeptideID = tmp02.PeptideID ENDSQL $sth->execute; print join("\t", @{ $sth->{NAME} } ), "\n"; while ( my $row = $sth->fetchrow_arrayref ) { print join("\t", @$row ), "\n"; }

Update: Also make sure to read up on the different kinds of SQL JOINs to see the difference between those and which one is appropriate for your case.

Replies are listed 'Best First'.
Re^2: match two files
by yueli711 (Sexton) on Dec 09, 2020 at 18:17 UTC
    Hello haukex, Thank yo so much for your great help! Thank you again! Best, Yue