Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: open input files once

by Marshall (Canon)
on Jul 14, 2020 at 10:44 UTC ( [id://11119295]=note: print w/replies, xml ) Need Help??


in reply to open input files once

I am not quite sure if I replicated your expected output.
Read file1 into data struct leaving placeholder for the 2nd value in file2.
Read file2, replace placeholder with 2nd value if key exists otherwise make a 0 for 1st value.
Fiddle with this to get what you want. Basic idea is only read each file once.

Update: Below I used a single string, but an anon array of 2 elements would also get the job done.

use strict; use warnings; use Inline::Files; use Data::Dumper; $|=1; my %data; while (<FILE1>) { next unless /^\S/; #skip blank lines my (@tokens) = (split( " ",$_))[0,1,2,3,6]; my $value = pop @tokens; $data{"@tokens"} = "$value 0"; } while (<FILE2>) { next unless /^\S/; #skip blank lines my (@tokens) = (split( " ",$_))[0,1,2,3,6]; my $value2 = pop @tokens; if ($data{"@tokens"}) { $data{"@tokens"}=~ s/0$/$value2/; } else { $data{"@tokens"} = "0 $value2"; } } foreach (sort keys %data) { print "$_: $data{$_} \n"; } =prints: chr10 181243 196457 1: 1 0 chr10 181243 225933 1: 4 3 chr10 181500 225933 1: 15 7 chr10 226069 255828 1: 53 38 chr10 255948 267233 1: 2 0 chr10 255989 267134 1: 65 29 chr10 255989 282777 1: 14 15 chr10 264440 267134 1: 1 0 chr10 267297 282777 1: 0 44 chr10 94055 94554 2: 1 1 chr10 94055 94743 2: 0 1 chr10 94666 94743 2: 0 3 chr10 94853 95347 2: 0 2 chr10 95850 122380 2: 1 0 =cut __FILE1__ chr10 94055 94554 2 2 1 1 0 23 chr10 95850 122380 2 2 1 1 0 15 chr10 181243 196457 1 1 1 1 0 21 chr10 181243 225933 1 1 1 4 0 21 chr10 181500 225933 1 1 1 15 0 25 chr10 226069 255828 1 1 1 53 0 22 chr10 255948 267233 1 1 1 2 0 12 chr10 255989 267134 1 1 1 65 0 25 chr10 255989 282777 1 1 1 14 0 23 chr10 264440 267134 1 1 1 1 0 25 __FILE2__ chr10 94055 94554 2 2 1 1 0 21 chr10 94055 94743 2 2 1 1 0 20 chr10 94666 94743 2 2 1 3 0 20 chr10 94853 95347 2 2 1 2 0 25 chr10 181243 225933 1 1 1 3 0 21 chr10 181500 225933 1 1 1 7 0 10 chr10 226069 255828 1 1 1 38 0 22 chr10 255989 267134 1 1 1 29 0 24 chr10 255989 282777 1 1 1 15 0 23 chr10 267297 282777 1 1 1 44 0 24

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11119295]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (None)
    As of 2024-04-25 01:43 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found