perl file manipulation

pal2k
pal2k used Ask the Experts™
on
Hello folks,

Actually I posted this question before. But I did not know how to put my question back to the comments I got. So I am putting it here.

Question/help:
I have a csv file
File name : input_file.csv

field1, field2
a1,b1
a2,b2
a3,b3
a4,a4
a5,a5

I have mentioned field1, field2 just for understanding that the file has 2 fields.

 In the above file field1 and field2 are the columns in a csv file.
just for understanding, a1 is the first value in line1 in field1(column) and b1 is field2(column) value in line1 respectively.  
I am looking for a matching criteria below:
By looking we can say that b1,b2,b3  are values in filed2(column) which are not found in field1(column). So I need print these not matched values of field2 in field1 to a output file. Probably I think I need help on looping foreach . Like every value in  if ($field1 ne $field2) which should be looped to get the resulting set. If I can get help in completing the code it would be great.

I have framed the condition with ne for not matching case
#!/usr/bin/perl
$source_file = 'C:\\input_file.csv';
open (FH, $source_file) || die ("Could not open $source_file!");
while ($line = <FH>)
{
  chomp($line);
  ($field1,$field2) = split ',', $line;
 
  print "$field1, $field2\n";
 
  if ($field2 ne $field1)
  # need help for looping all values in field2 to chek with all values with field1
  {print "$field2\n";}
  }
close (FH);
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:

#!/usr/bin/perl
$source_file = 'c:\\input_file.csv';
open (FH, $source_file) || die ("Could not open $source_file!");
 
my (%listf1, %listf2);
 
while ($line = <FH>)
{
  chomp($line);
  ($field1,$field2) = split ',', $line;
 
  $listf1{$field1} = $field1;
  $listf2{$field2} = $field2;
 
}
close (FH);
 
foreach $key (sort keys %listf2) {
  if ( !defined( $listf1{$key} ) ) {
    print "$key\n";
  }
}

Open in new window

Author

Commented:
Awesome....oocyte...!!!. Really Appreciate you was the quick solution.....

Exactly what I need and it was not complicated.

Great....

Thanks a lot "oocyte"

Commented:
You are welcome =)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial