Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: problem in looping

by hbm (Hermit)
on Aug 13, 2012 at 19:10 UTC ( #987184=note: print w/ replies, xml ) Need Help??

in reply to problem in looping

This should get you close, though it assumes the two input files have the same number of lines.

use strict; use warnings; my @f1 = get_lines("1.txt"); my @f2 = get_lines("2.txt"); open(my $out1, ">", "3.txt") or die; open(my $out2, ">", "4.txt") or die; for my $i (0..$#f1){ if (abs($f1[$i]->[1]-$f2[$i]->[1])>1){ print $out1 "$f1[$i]->[0]\n"; print $out2 "$f2[$i]->[0]\n"; } } close $out1; close $out2; sub get_lines { my $file = shift; my @lines; open(my $fh, "<", $file) or die("Unable to open $file: $!"); while(<$fh>){ chomp; /(\d+)$/; push @lines, [ $_, $1 ]; } close $fh; return @lines; }

Comment on Re: problem in looping
Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (9)
As of 2016-02-08 09:24 GMT
Find Nodes?
    Voting Booth?

    How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?

    Results (271 votes), past polls