Help for this page

Select Code to Download


  1. or download this
    mergefile.1
    
  2. or download this
    15    20    foo
    22    30    bar
    30    33    baz
    14    22    fubar
    
  3. or download this
    mergefile.2
    
  4. or download this
    alpha    baz    17.30
    gamma    foobar    22.35
    gamma    bar    19.01
    delta    fromish    33.03
    sigma    bear    14.56
    
  5. or download this
    mergefile.out
    bar     22      30      gamma   19.01
    ...
    foobar  null    null    gamma   22.35
    fromish null    null    delta   33.03
    fubar   14      22      null    null
    
  6. or download this
    #!/usr/bin/perl -w
    use strict;
    use warnings;
    
  7. or download this
    open F1, 'sort -k3 mergefile.1|' or die "opening file 1";
    open F2, 'sort -k2 mergefile.2|' or die "opening file 2";
    
  8. or download this
    open OUF, '>', 'mergefile.out' or die "opening output file";
    
  9. or download this
    my @in1;
    my @in2;
    
  10. or download this
    sub getrec1 {
            @in1 = ();
    ...
                    chomp $in1[2];
            }
    }
    
  11. or download this
    sub getrec2 {
            @in2 = ();
    ...
                    chomp $in2[2];
            }
    }
    
  12. or download this
    sub write1 {
            print OUF "$in1[2]\t$in1[0]\t$in1[1]\tnull\tnull\n";
            getrec1;
    }
    
  13. or download this
    sub write2 {
            print OUF "$in2[1]\tnull\tnull\t$in2[0]\t$in2[2]\n";
            getrec2;
    }
    
  14. or download this
    sub writeboth {
            print OUF "$in1[2]\t$in1[0]\t$in1[1]\t$in2[0]\t$in2[2]\n";
            getrec1;
            getrec2;
    }
    
  15. or download this
    # Prime the pump
    getrec1;
    getrec2;
    
  16. or download this
    while (1) {
            last if $#in1<0 and $#in2<0;
    
  17. or download this
            if ($#in1<0 or $#in2<0) {
                    # Only one file is left...
    ...
                    write2;
            }
    }
    
  18. or download this
    bar    null    null    gamma    19.01
    bar    22    30    null    null
    ...
    foobar    null    null    gamma    22.35
    fromish    null    null    delta    33.03
    fubar    14    22    null    null
    
  19. or download this
    I altered the line "chomp $in2[2];" to read "chomp $in2[1];"