How to fix my code in Perl?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to fix my code in Perl?
# 1  
Old 08-02-2013
How to fix my code in Perl?

Hi Perl users,

Could you help me how to fix my code so I can get the expected output as seen below?
is there missing in my program?

Thank You

Code:
#!/usr/bin/perl -w

use strict;

open (FH, "< file1.log") or die $!;
open (FL, "< file2.log") or die $!;

my $file = "result.log";

my ($afile, $bfile);
foreach my $lines (<FH>){
  chomp $lines;
  ($afile, $bfile) = split (/\s+/,$lines);
   if (open(FC, "> $file")){
     while (<FL>){
        if ($_ =~ /$afile/){
          print FC $_;
        }
     }
   }
   close (FC);               
}

close (FH);
close (FL);

__DATA__
file1.log:
unix1 test
unix2 test

file2.log:
unix1 halo halo
unix2 halo halo
unix3 halo halo

------

Code:
Unexpected result:
unix1 halo halo

Code:
The expected result:
unix1 halo halo
unix2 halo halo

---------- Post updated 08-03-13 at 05:34 AM ---------- Previous update was 08-02-13 at 10:47 PM ----------

Done by my self. Here is the code:
Code:
#!/usr/bin/perl -w  use strict;  open (FH, "< file1.log") or die $!; open (FL, "< file2.log") or die $!; my @temp = <FL>; my $file = "result.log";  my ($afile, $bfile); foreach my $lines (<FH>){   chomp $lines;   ($afile, $bfile) = split (/\s+/,$lines);    if (open(FC, "> $file")){      foreach (@temp){         if ($_ =~ /$afile/){           print FC $_;         }      }    }    close (FC);                }  close (FH); close (FL);

# 2  
Old 08-05-2013
Yes, need to rewind input on inner loop.
Login or Register to Ask a Question

Previous Thread | Next Thread

2 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help fix my garbage - File Split Program in Perl

Hi, I have the following, it doesn't work and I know it's crap code. The objective is to split a file with a givin number of codes such as: 01,02,03,...,99 Then return all records with each seperate identifier in a new file. The files being split have lrecl=500, recfm=F, and I... (4 Replies)
Discussion started by: mkastin
4 Replies

2. UNIX for Dummies Questions & Answers

How to fix :[too many arguments error in code

I am getting a :; then echo "Enter zero or one file" echo "You must use a valid directory" echo "Current directory is:" pwd exit 0 fi #Flag Variable flag=1 #Code for no arguments if ; then for filename in * do if ; then ... (2 Replies)
Discussion started by: Brewer27
2 Replies
Login or Register to Ask a Question