perl: problem in looping! how to get rid


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl: problem in looping! how to get rid
# 1  
Old 08-13-2012
perl: problem in looping! how to get rid

Hi i just want to open 2 files and find difference between cond1[2] and cond2[2] and if the difference is greater than or equal to some number say 2 print the lines again in 2 different files.
Code:
file 1 (1.txt) 
aqw dfr 34 
poilo ggg 98 
file 2  (2.txt)
qww asd 28 
poilo ggg 97

Code:
open FILE1,"1.txt" or die "can't open file 1";
open FILE2,"2.txt" or die "can't open file 2";
open(w1,">3.txt");
open(w2,">4.txt");
while (my $line1 = <FILE1>) {
chomp $line1;
my @cond1 = split("\t" , $line1);
print "$cond1[2]\n";
while (my $line2 = <FILE2>) {
chomp $line2;
my @cond2 = split("\t" , $line2);
print "$cond2[2]\n";
if(abs($cond1[2]-$cond2[2])>=2)
{
print w1 "$line1\n";
print w2 "$line2\n";
}
}
}
close FILE1;
close FILE2;
close w1;
close w2;

when i try this i am not able to get the output in proper way i mean my output should look like this
Code:
3.txt 
aqw dfr 34
4.txt 
qww asd 28

# 2  
Old 08-13-2012
Hi,

If what you want is to compare line 1 of each file then line 2 of each file etc, then the code is wrong.
The code you show compare line 1 of file1 with all lines of file2 and then same for line 2.

So if i use the same code and make minimal change to your code the right one should be:
Code:
open FILE1,"1.txt" or die "can't open file 1";
open FILE2,"2.txt" or die "can't open file 2";
open(w1,">3.txt");
open(w2,">4.txt");

while ((my $line1 = <FILE1>) && (my $line2 = <FILE2>)) {
    chomp $line1;
    chomp $line2;
    my @cond1 = split("\t" , $line1);
    print "$cond1[2]\n";
    my @cond2 = split("\t" , $line2);
    print "$cond2[2]\n";
    if(abs($cond1[2]-$cond2[2])>=2)
    {
        print w1 "$line1\n";
        print w2 "$line2\n";
    }
}
close FILE1;
close FILE2;
close w1;
close w2;

This User Gave Thanks to Chirel For This Post:
# 3  
Old 08-13-2012
Try out the below code to read one line from first file and second file and do your check:
Code:
open FILE1,"1.txt" or die "can't open file 1";
open FILE2,"2.txt" or die "can't open file 2";
open(w1,">3.txt");
open(w2,">4.txt");

while ( ! eof( FILE1 ) ) {
  # Read a line from 1st file
  my $file1_buffer = readline( *FILE1 );
  chomp $file1_buffer;
  my @cond1  =  split( "\t", $file1_buffer );
  print "$cond1[2]\n";

  # Read a line from 2nd file
  my $file2_buffer = readline( *FILE2 );
  chomp $file2_buffer;
  my @cond2  =  split( "\t", $file2_buffer );
  print "$cond2[2]\n";

  if ( abs( $cond1[2] - $cond2[2] )  >=  2 ) {
    print w1 "$file1_buffer\n";
    print w2 "$file2_buffer\n";
  }
}
close FILE1;
close FILE2;
close w1;
close w2;

This User Gave Thanks to spacebar For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Looping problem

I need help. I am trying to get this script to send out only one email not multiple emails of the abend. Currently it will send me one then and ther with the first and second one then another email with the first second and third abend and so on. I only want one email sent. ... (2 Replies)
Discussion started by: bbcarosi
2 Replies

2. Shell Programming and Scripting

Looping in Perl based on defined keys in Map

Hello All, I am writing the below script where it will connect to database and returns the results. #!/sw/gcm/perl510/bin/perl use SybaseC; &openConnection; &loadvalues; sub openConnection { $dbproc = new SybaseC(SYDB}, $ENV{DBDFLTUSR}, $ENV{DBDFLTPWD}); if... (2 Replies)
Discussion started by: filter
2 Replies

3. Shell Programming and Scripting

Problem with Looping

Hi, guys, What I want is exactly shown below (I modified the former image and it looks like clearer.) https://lh6.googleusercontent.com/-EG8SKkrWEvc/Ube9e-jDiHI/AAAAAAAAAOM/hFNT0UqQPWE/s512/Linux_Study_20130611_001.jpg And with some guys' help, I made it. My script is below: #!/bin/bash #... (20 Replies)
Discussion started by: franksunnn
20 Replies

4. Shell Programming and Scripting

looping problem

I have been trying to come up with a program that can do this: Say I have a file named "sir" with a single field; 10 229 288 35 83 47 3 I want to create a file "gen" with three fields with the data in file "sire" listed in field 1 while field 2 and 3 are just 1 each like this: SPARSE... (1 Reply)
Discussion started by: iconig
1 Replies

5. Solaris

SVM Solaris 8 Problem. Metastat output looping

Hi friends, I'm newbie to SVM. Just wanna try installed it on one of our server (to do mirroring for disk0 and disk1) but i think im lost until now. :( the steps i've taken is as below:- 1.prtvtoc /dev/rdsk/c1t0d0s2 | fmthard -s - /dev/rdsk/c1t1d0s2 2.metadb -a -c 3 -f c1t0d0s7... (3 Replies)
Discussion started by: kronenose
3 Replies

6. Shell Programming and Scripting

perl: looping through the output of a 'system' command

Hi there could anybody point me in the right direction when it comes to looping through the output of a system command in perl (i.e. df -k) doing a test against each line to see if it matches? for example if i have a df -k output like this and I wanted to grab the lines that matched "sda" or... (3 Replies)
Discussion started by: rethink
3 Replies

7. Shell Programming and Scripting

Problem with looping the directories

Hi all, I have a directory which has many sub-directories. Now, I want to check the space of each dir and redirect the output in a file if space exceeds the limit. I have already done it, but the way I did is not very good. I just listed the directories and awked the last column to get the... (5 Replies)
Discussion started by: naw_deepak
5 Replies

8. Shell Programming and Scripting

Problem with looping construct

Hi all I have tried to search for this, but keep getting a MySQL db connect error, so am posing the question here, and taking a risk of incurring the wrath of the mods with my first post... I have the following test script: #!/bin/bash HTTPD=`/bin/ps -axcu | /usr/bin/grep httpd... (6 Replies)
Discussion started by: mikie
6 Replies

9. Shell Programming and Scripting

Awk: looping problem!

I am having a problem with awk when I run it with a loop. It works perfectly when I echo a single line from the commandline. For example: echo 'MFG009 9153852832' | awk '$2 ~ /^0-9]$/{print $2}' The Awk command above will print field 2 if field 2 matches 10 digits, but when I run the loop... (5 Replies)
Discussion started by: cstovall
5 Replies

10. Shell Programming and Scripting

Perl question - looping through an array of hashrefs

I have an array of hashrefs that look like the following: my @LAYOUT = ( {SQL_1 => "select count (*) FROM prospect WHERE PROCESS_DATE = To_date('INSERT_DATE_HERE', 'mm/dd/yyyy') and tiff_filename is not null ... (2 Replies)
Discussion started by: kregh99
2 Replies
Login or Register to Ask a Question