Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: problem in looping

by hbm (Hermit)
on Aug 13, 2012 at 19:10 UTC ( [id://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; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-16 05:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found