Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Holy Monks of Perl,
I am having a lot of problems trying to compare 2 C files.
firstly let me show you the 2 files:
<Text1.c>
checkeof
GetFile

<stub.c>
GetFile
SerialWaitForChar
strcpy
checkeof
DownloadImg

What i need to do is to remove all occurances in Text1.c from stub1.c. But I keep getting what I had in stub.c.<\p>

I think maybe it because of the hash that i am using. The code that i am using is as follows:

%hash2; $done; open (Global, "text1.c") for $fish(test1){ open (Local, "stub.c") or die "Can't open stub.c :$!\n"; for $local(<Local>){ $local =~ s/\n//; unless ($fish eq $local){ $hash2{$local}++; } close Local; } } foreach $done ( keys %hash2){ print "$done"; }

So is there anyway for me to rectify this?? Thanks in adavnce!!!

Replies are listed 'Best First'.
Re: Comparing files
by etcshadow (Priest) on Oct 16, 2003 at 02:38 UTC
    OK, I can't read your code easily, and I don't really feel like trying. If what you want is "all lines found in file1 which are not in file2" then here you go:
    #!/usr/bin/perl use strict; my ($source_filename,$exclusion_filename) = @ARGV; # read each line of exclusion file and store as a hash-key (for quicke +r look-up) open EXCLUSION, "$exclusion_filename" or die "Could not open file of e +xcluded lines: $!\n"; my %exclude = map { ($_ => 1) } <EXCLUSION>; close EXCLUSION; open SOURCE, "$source_filename" or die "Could not open source file: $! +\n"; while (<SOURCE>) { print unless $exclude{$_}; } close SOURCE;
    Results:
    [me@host scratch]$ cat > a asdf asdf fdsa asd ddd d [me@host scratch]$ cat > b ddd as [me@host scratch]$ perl tmp.pl a b asdf asdf fdsa asd d [me@host scratch]$

    ------------
    :Wq
    Not an editor command: Wq
Re: Comparing files
by pg (Canon) on Oct 16, 2003 at 02:11 UTC

    I have waited quite a while to see whether anyone replies, but up to now it is a no.

    There is something that discourages people from replying it. Is this your real code? Have you really tested it? When I ran it, it gave me syntax error.

    The other thing discourages me is that your code is not indented.

    I suggest you to modify your post a little bit, so someone CAN help.

Comparing 2 C files
by Anonymous Monk on Oct 16, 2003 at 03:51 UTC
    hi,
    I have a question regarding how to compare 2 different C source files(e.g. Text1.c, stub.c), which contain names of function calls.

    My objective is to compare these 2 source file and get rid of any occurances of the function names inside Text1.c(reference file) from stub.c(working file)

    The problem I have is that my code doesn't seem to be able do that. The script seems to just copy the all same function calls that I have in stub.c, without removing any occurance of the repeated function calls found in Text1.c

    #!/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; #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++ } #opening file for writing open (Done, ">sim.c") or die "Can't open sim.c :$!\n"; #initiating a new hash and a string for later use %hash2; $done; #a for loop to help popping all the elements in array. for (0..6){ $fish = pop @array; $fish = pop @array; #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; } } #print each key of the hash to verify result foreach $done ( keys %hash2){ print "$done\n"; }

    Note to monk pg and monk etcshadow: I am the idiot who posted the previous thread under Anonymous Monk. A thousand apologies to monk pg and monk etcshadow. I should have posted the full code

      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...


      $;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/
        hi,
        I really deserved to be shot and hanged for not identing my code!! A million apologies!

        Thanks for the analysis of my codes. I have learn quite a bit from that(notably the use of chomp to get rid of new line and the assigning of key values into the array, didn't think of that before!).

        However, for the last part, I wish to clarify some things with you. Because what I really want to get the functions that doesn't matches, to be printed out and not the count of the number of functions that doesn't match.

        I did tried what you suggested. And I traced the problem to the following code :

        unless ($fish eq $local) { $hash2{$local}++; }
        because a hash would keep unique cases of whatever that is assigned to it, everytime I tried to change it, this would happen:

        Test1.c
        function1<br> function2<br>
        stub.c
        function1<br> stubfunction1<br> function2<br> stubfunction2<br>
        now we have 2 files to work with and the actual output is:

        function1<br> stubfunction1<br> function2<br> stubfunction2<br><br>
        when actually what I wanted is :

        stubfunction1<br> stubfunction2<br><br>
        So is there any other things that I can try to get the output that I want.
      For the love of god, indent your code!
      if( 1 ) { #INDENTED STUFF if( 0 ) { #MORE INDENTED STUFF } }