#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    45
    Rep Power
    5

    How to print intermediate range values from two file columns


    Hi all,

    I am trying to check intermediate range values from two file columns.
    My two input files are like this:

    (file1.txt)

    a 11-23
    b 33-39
    c 40-45
    d 48-58


    & (file2.txt)

    33-39
    40-42
    43-46
    51-52

    From it, I want to match the file2 values to that of file1 (2nd column) with intermediate ranges of values, I want the output to be like:

    b 33-39
    c 40-42, 43-45
    d 45-46, 51-52


    Any help would be appreciated. Thanks.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Apr 2009
    Posts
    2,231
    Rep Power
    1296
    What have you tried?

    What errors/warnings is your code producing?

    How does the output of your script differ from what you expect?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    45
    Rep Power
    5
    Originally Posted by FishMonger
    What have you tried?

    What errors/warnings is your code producing?

    How does the output of your script differ from what you expect?

    I hav tried:
    Code:
    @ARGV or die "No input file specified";
    
    open my $first,  '<', $ARGV[0] or die "Unable to open input file: $!";
    open my $second, '<', $ARGV[1] or die "Unable to open input file: $!";
    
    print scalar(<$first>);
    
    my $secondHeader = <$second>;
    
    while (<$first>) {
        @cols = split /\s+/;
        $p1   = $cols[1]; 
        $p2   = $cols[2]; 
    
        my $secondLine = <$second>;
        if ( defined $secondLine ) {
            @sec = split( /\s+/, $secondLine );
    		print join( "\t", @cols ), "\n" if ( $p1>=$sec[0] && $p2<=$sec[1] || $p1<=$sec[0] && $p2>=$sec[1] ); 
        }
    }
    Which gives me output:
    a 11-23
    b 33-39
    c 40-45
    d 46-58
    I am stuck about what argument I should give in the last line to get the proper output.. Plz help
  6. #4
  7. wizard
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2009
    Location
    The Great White North
    Posts
    83
    Rep Power
    140
    OK, first put this at the top of all your scripts. You can learn what they to by searching for them in perldoc.perl.org

    Code:
    use strict;
    use diagnostics;
    use autodie;
    The easiest way to do this is to create a hash from file 1 where the key is the thing you want to search for. Try creating only this part first and post the code.
    Don't stop where the ink does.

IMN logo majestic logo threadwatch logo seochat tools logo