Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

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; }

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 making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2016-07-25 08:21 GMT
Find Nodes?
    Voting Booth?
    What is your favorite alternate name for a (specific) keyboard key?

    Results (222 votes). Check out past polls.