Hello mao9856,

One possible way it to store keys of both files based on id e.g. file keys qw(ABS0056 ABS0057 ABS0058) etc as you read your file push them into an array. Create a hash from the second file with key the id and value the name e.g. $hash{"ABS0056"} = "SAM"; while you read the file.

Then simply compare keys and find the common ones, iterate over the hash with the common keys remove the ones that are not common and voila. :D

Hope this helps you to proceed. Show us the effort of resolving your problem not just this is what I want to do do it for me.

Update: Module to compare arrays List::Compare.

Update2: Maybe this small sample of code will get you to the right direction. This is more or less 3/4 of the solution. There are many ways to approach your problem but this is one out of them. Sample of code below:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; sub cleanString { my ( $str ) = @_; my ( $first , $second ) = split( / / , $str ); return substr( $second , 1 , length($second) -2 ); } my $File1 = 'file1.txt'; open (my $fh1, "<", $File1) or die "Error opening file1: $!\n"; my @keys; my $keyRef; while (<$fh1>) { chomp; next unless $_ ne ''; $keyRef->{$_} = 1; push @keys, $_; } close $fh1 or warn "Could not clode file1: $!\n"; # print Dumper $keyRef; # print Dumper \@keys; my $File2 = 'file2.txt'; open (my $fh2, "<", $File2) or die "Error opening file2: $!\n"; my %hash; while (<$fh2>) { chomp; next unless $_ ne ''; my @elements = sort( map { s/^\s+//; # strip leading spaces s/\s+$//; # strip trailing spaces $_ # return the modified string } split ';', $_ ); # print Dumper \@elements; $hash{cleanString($elements[0])} = cleanString($elements[1]); } close $fh2 or warn "Could not clode file1: $!\n"; print Dumper \%hash; __END__ $ perl test.pl $VAR1 = { 'ABS0059' => 'JOE', 'ABS0060' => 'MARY', 'ABS0057' => 'BILL', 'ABS0061' => 'STEPHAN', 'ABS0065' => 'RONIE', 'ABS0056' => 'SAM' };

BR / Thanos

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Re: print data with matching id from two different files by thanos1983
in thread print data with matching id from two different files by mao9856

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.