Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks


please humbly excuse my ignorance but i am new to perl and I need to open two files(tab delimited, so i can
use split). I then open file1 and extract first data and look for a match in file2, when i find a match in file2
i take data 2 from file1 and insert it before data1 in file 2. NOTE i will be only ever looking at the first
string in file2 as i have already sorted both files. then i want to write the result(i expect the best here
is to make a temp file and then delete the old file).


many thanks in advance for assistance in the matter

regards Anthony

Replies are listed 'Best First'.
Re: matching data with 2 files
by Skeeve (Parson) on Jul 16, 2003 at 11:54 UTC
    I'm not quite sure, whether or not I understand it right. It sounds to me like: My teacher said we have to merge two sorted files into a new sorted file ;-)

    Please show us what you already tried and where you failed.

Re: matching data with 2 files
by Anonymous Monk on Jul 16, 2003 at 12:01 UTC
    Ok after reading some old posts i have tried the
    following, but alas it did not work

    open (LIST1, $list1) or die "cannot open $list1";
    open (LISt2, $list2) or die "cannot open $list2";

    while (<LISt1>)
    {
    #get all the variables for line from LIST1

    my ($c,$d,$e,$f,$g,$h) = split(/"\t"/);
    $flag=0; #reset the flag

    my %axepar_list = ();
    while (<AXEPAR>)
    {
    ($a, $b) = split(/"\t"/);
    $axepar_list{$a} = $b;
    }

    if (exists $axepar_list{$c})
    {
    $block_name = $axepar_list{$c};
    print "$block_name $d $e $f $g\n";
    $flag=1;
    }
    }

    close AXEPAR;
    close LASLIST;
Re: matching data with 2 files
by Anonymous Monk on Jul 16, 2003 at 12:11 UTC
    sorry guys here is the corrected file (copy paste is not so good



    open (LIST1, $list1) or die "cannot open $list1";
    open (LISt2, $list2) or die "cannot open $list2";

    while (<LIST1>)
    {
    #get all the variables for line from LIST1

    my ($c,$d,$e,$f,$g,$h) = split(/"\t"/);
    $flag=0; #reset the flag

    my %list2key= ();
    while (<LIST2>)
    {
    ($a, $b) = split(/"\t"/);
    $list2key{$a} = $b;
    }

    if (exists $list2key{$c})
    {
    $name = $list2key{$c};
    #print out to screen as a test then to a file later print "$name $d $e $f $g\n";
    $flag=1;
    }
    }

    close LIST2;
    close LIST1;
      open (LIST1, $list1) or die "cannot open $list1"; open (LIST2, $list2) or die "cannot open $list2"; while (<LIST1>) { #get all the variables for line from LIST1 my ($c,$d,$e,$f,$g,$h) = split(/"\t"/); # do you mean "\t" or \t? # are there " in the file? $flag=0; #reset the flag # which flag? my %list2key= (); while (<LIST2>) { ($a, $b) = split(/"\t"/); $list2key{$a} = $b; } # This will read your second file once for the # first line of your first file # As soon as you have the second line of you first # file, it will forget about the second file! # Maybe you should move this loop out of the # LIST1-loop if (exists $list2key{$c}) { $name = $list2key{$c}; #print out to screen as a test then to a file later print "$name $ +d $e $f $g\n"; $flag=1; } }
      Does this help you understanding the script you copied?
        Thanks Skeeve, moving the inside loop out worked
        once i stopped confusing my key and hash values
        below is the final code, a bit clinky but it works


        open (LIST1, $list1) or die "cannot open $list1";
        open (LIST2, $list2) or die "cannot open $list2";

        my %list2key= ();
        while ($line1 = <LIST1>)
        {
        my ($a, $b) = split(/\t/,$line1);
        $list2key{$a} = $b;
        }

        while ($line2 = <LIST2>)
        {
        #get all the variables for line from LIST2

        my ($a, $b, $c, $d, $e, $f) = split(/\t/,$line2);
        # do you mean "\t" or \t? are there " in the file?

        # This will read your second file once for the
        # first line of your first file
        # As soon as you have the second line of you first
        # file, it will forget about the second file!
        # Maybe you should move this loop out of the LIST1-loop

        if (exists $list2key{$a})
        {
        $name = $list2key{$a};
        #print out to screen as a test then to a file later
        print "$name $b $c $d $e $f\n";
        }
        }

        close LIST2;
        close LIST1;
Re: matching data with 2 files
by Mago (Parson) on Jul 17, 2003 at 13:20 UTC
    #!/usr/bin/perl use strict; use warninigs; my $fileList1 = $ARGV[0]; my $fileList2 = $ARGV[1]; my $line1; my $line2; open(FILE1, "< $fileList1"); open(FILE2, "< $fileList2"); open(TEMP, "> $fileTemp"); while ($line1 = <FILE1>) { chomp $line1; my @param1 = split("\t", $line1); while ($line2 = <FILE2>) { chomp $line2; my @param2 = split("\t", $line2); if ($param1[0] eq $param2[0] { print TEMP $param2[0] . "\t" . $param1[1]; } else { print TEMP $param2[0] . "\t" . $param2[1]; } } } close FILE1; close FILE2; close TEMP; rename($fileTemp, $fileList2);