LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl Script needed to be reversed to output matching, not non-matching (https://www.linuxquestions.org/questions/programming-9/perl-script-needed-to-be-reversed-to-output-matching-not-non-matching-819686/)

0bfuscated 07-13-2010 06:23 PM

Perl Script needed to be reversed to output matching, not non-matching
 
I am having a rough time attempting to reverse this tiny script to outfile matching fields. It only outfiles the non-matching. I tried playing with the regular expressions and of course it did not work. Could someone give me some insight as to how to make this reverse its role and outfile matching fields?

Thanks!

Code:

#!/bin/perl
use strict;
use warnings;

my $f1 = 'file2';
my $f2 = 'hosts';
my $outfile = 'hosts1';
my %results = ();

open FILE1, "$f1" or die;
while(my $line = <FILE1>){
  $results{$line}=1;
}
close(FILE1);

open FILE2, "$f2" or die;
while(my $line =<FILE2>) {
  $results{$line}++;
}
close(FILE2);


open (OUTFILE, ">$outfile") or die;
foreach my $line (keys %results) {
  print OUTFILE $line if $results{$line} == 1;
}
close OUTFILE;


Sergei Steshenko 07-13-2010 06:51 PM

Quote:

Originally Posted by 0bfuscated (Post 4032376)
I am having a rough time attempting to reverse this tiny script to outfile matching fields. It only outfiles the non-matching. I tried playing with the regular expressions and of course it did not work. Could someone give me some insight as to how to make this reverse its role and outfile matching fields?

Thanks!

Code:

#!/bin/perl
use strict;
use warnings;

my $f1 = 'file2';
my $f2 = 'hosts';
my $outfile = 'hosts1';
my %results = ();

open FILE1, "$f1" or die;
while(my $line = <FILE1>){
  $results{$line}=1;
}
close(FILE1);

open FILE2, "$f2" or die;
while(my $line =<FILE2>) {
  $results{$line}++;
}
close(FILE2);


open (OUTFILE, ">$outfile") or die;
foreach my $line (keys %results) {
  print OUTFILE $line if $results{$line} == 1;
}
close OUTFILE;


Well, your post appears obfuscated to me. I.e. I can't understand what you want to achieve. I think you should more clearly explain what are the inputs, what is the desired output and what is the actual output.

To explain also means showing examples trough print statements and/or test cases.

Lost_Oracle 07-20-2010 10:51 AM

0bfuscated,

It looks like you might be trying to print out all of the matching lines from two compared text files. Is that the case?

If so, there is likely a better way to do it. Most langauges have some sort of string compare function that returns a 0 if the two strings are identical. I'm sure perl has something similar. That might save you some hassle. A few if statements might do the trick in printing those that match rather than those that don't. Try setting your $results{$line} = 0 if they don't match. It'll make the if statements much easier to deal with.

If you want to match all matching lines between the two files, and not just those that are in identical spots in the files, you could loop through the file.
Note: this will take much more processing power and time, especially for very large files.

A sample of your input and what you want to get for output would be very helpful.


Lost


All times are GMT -5. The time now is 06:59 PM.