in reply to Comparing 2 C files
in thread Comparing files
The code posted by etcshadow in the other thread seems to me like it ought to do what you want, or, at least, it ought to do what you say you want, namely, compare two files and get rid of lines in the one that occur in the other.
Read on for an analysis of your code...
#!/user/bin/perl -w #note: %hash1 contains modified functions, #which has been extracted from a C source file(Test1.c). #this hash is used for referencing purposes @array = %hash1;
For testing purposes, did you print out the contents of %hash1 to make sure it contains what you think it does? Also, why exactly are you assigning both the keys and the values from the hash to an array?
#initiate a loop counter $loop = 0; #using for loop to get rid of new line in the array for (@array){ $array[$loop] =~ s/\n//; $loop++ }
I had to read this twice to even understand what it does, even though what it does is very simple. It's not clear at a glance that the element being modified is the current one being iterated over by the loop. Why not just chomp for @array or @array = map {s/\n//; $_} @array or something similarly straightforward? Even better, why not remove the newlines in the first place, when you read them from the file, before you store each one in the hash?
#opening file for writing open (Done, ">sim.c") or die "Can't open sim.c :$!\n";
That part's good.
#initiating a new hash and a string for later use %hash2; $done;
What "initiating" do you think this accomplishes? You don't set them to any value, and you don't scope them, and those are usually the only reasons to initialise a variable.
#a for loop to help popping all the elements in array. for (0..6){ $fish = pop @array; $fish = pop @array;
Okay, so you pop off a value, discard it, then pop off a corresponding key. It now seems very odd, since you are throwing away the values, that your earlier assignment was @array=%hash1. If you'd directly done @array = keys %hash1, you could have saved yourself a pop here, to say nothing of confusion. Even better, why not skip @array altogether, and change your for (0..6) to read for $fish (keys %hash1)? If you remove the newlines when you read the values into the hash like I suggested earlier, you can not only get rid of the whole loop for removing the newlines, but now you can also get rid of @array and make it more clear what you're doing here.
#opening of working file, which is to be compared with #the reference array(%hash1) open (Local, "stub.c") or die "Can't open stub.c :$!\n"; for $local(<Local>){ $local =~ s/\n//; #this is used to compare the 2 variable #if there is no match, assign it as a key to a hash unless ($fish eq $local){ $hash2{$local}++; } close Local; } }
The logic here is a little hard for me to follow, but as I understand it, what you're doing is counting how many of the keys from %hash1 don't exactly match each of the lines in the local file. That is, for each line in stub.c, %hash2 holds a count of how many of the keys from %hash1 are different from this line in stub.c. Is that what you intended? It's certainly not what you described in your question up above.
#print each key of the hash to verify result foreach $done ( keys %hash2){ print "$done\n"; }
For debugging purposes, until you get it working the way you want, you really ought to print the values, as well as the keys:
print join "\n", map {"$_ => $hash2{$_}"} keys %hash2;$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Comparing 2 C files
by Anonymous Monk on Oct 16, 2003 at 04:52 UTC | |
by jonadab (Parson) on Oct 16, 2003 at 14:12 UTC |