LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-20-2008, 02:06 PM   #1
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Rep: Reputation: 15
Exclamation Question about comparing one file to another in Perl


I have a perl project that take a csv file and then process it. It will take the records and put them in different files depending on the results of the processing, such as, if the record is to many pages it will place the record in a bad record file and if the record is printed by an exempted staff member it will place the record in the staff file. There is a separate file that holds the exempted staff members

The problem I have is when I try to compare the record to the exempted staff file. Here is how I do it so far:

Code:
open(IN, "etc/except.txt");
(IN) || die "Unable to open exceptions!\n";
while(<IN>)  {
  chop;
  tr/A-Z/a-z/;
  push(@except, $_);
}
close(IN);
I then open the other files for the process and here is the section that does not work correctly:

Code:
while(<IN>)  {

$count = @except;
for ($i=0; $i<$count; $i++) {
    	if ($a[5] = $except[$i]) {
		print STAFF "$_\n";
		next;
	}
    }

}

obviously there are other statements that process the rest of the records, but they work just fine. For some reason this ends up placing all record into the staff file. How do I get this fixed?
 
Old 11-20-2008, 02:23 PM   #2
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
instead of = try eq or == if its a numeric record, = is for assignment and evaluates true
 
Old 11-20-2008, 02:39 PM   #3
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Original Poster
Rep: Reputation: 15
Exclamation

that worked, however is there anything I need to do other than that?

Like for ($i=0; $i<$count+1; $i++) { or something? It should not be cutting off the last name in the exempt file should it?
 
Old 11-20-2008, 03:23 PM   #4
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
$count = @except for the number of elements in the array should work just fine and your original loop should iterate through all the elements in except. You also probably want chomp instead of chop. You could make this more efficient by using a hash instead.


In your @except loading loop replace
push(@except, $_);

with
$except{$_} = 1;

and then eliminate that whole inner loop
Code:
$count = @except;
for ($i=0; $i<$count; $i++) {
    	if ($a[5] = $except[$i]) {
		print STAFF "$_\n";
		next;
	}
    }
and replace it with
Code:
if ($except{$a[5]} == 1) {
	print STAFF "$_\n";
}

Last edited by estabroo; 11-20-2008 at 03:24 PM. Reason: chomp
 
Old 11-20-2008, 05:26 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
First and foremost, use

use strict;
use warnings;

in the beginning of you program/module - they show you a lot of problems.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
file comparing and printing viveksnv Programming 11 02-28-2008 03:55 AM
comparing file xlordt Programming 5 11-19-2007 03:32 AM
Perl: comparing 2 files UnixKiwi Programming 14 07-05-2007 03:27 AM
comparing music file pantera Programming 2 07-01-2004 02:57 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration