PERL script help

Hi all

I wrote this, and am very new to PERL. Could someone show me what i might be doing wrong?? Here is my code:

open(DATA1,"ping1.txt");
open(DATA2,"ping2.txt");

$data1 = <DATA1>;
$data2 = <DATA2>;

while ($data1 || $data2) {
next if !strcmp($data1,$data2);
print "<DATA1";
print ">DATA2";

$data1= <DATA1>;
$data2= <DATA2>;

}

basically, i am running a ping and putting the results into a file. What I want to do is compare the 2 largest files and annotate the differences (if any)

the error i get is: undefined subroutine &main::strcmp called at filename.pl <DATA2> chunk1

SO I thought, hmm subroutine. So I tried declaring one before the next if before the { but I received even more errors.

But now I am lost...can anyone provide me some help?

Thanks

Joe

Unlike C, perl does not have a function called strcmp. To compare two strings, you just have to do this:

if($data1==$data2) {
--do whatever you want here--
}
else {
--do something else here--
}

strcmp in perl is

if( $data1 eq $data2 ){

}else{

}

Interesting, I sorta found that out, but at first I was using != for not equal. Error city. Anyway, so that is how my script looks. However, all that is output now is

<
>
<
>
<
>

Here is what my script looks like now:

open(DATA1,"ping1.txt");
open(DATA2,"ping2.txt");

$data1 = <DATA1>;
$data2 = <DATA2>;

while ($data1 || $data2) {
if ($data1 ne $data2) {

else print "&lt;$data1";
      print "&gt;$data2";
\}

$data1= <DATA1>;
$data2= <DATA2>;

}

Like I said, all i get now is
<
>
<
>
in a repeating pattern that never ends. Should I put an EOF in there somewhere? Another question is I KNOW the files are different because i made some changes to test them

I know I am overlooking something here...

Thank You for your inputs. :slight_smile:

Joe

Your code seems to work for me with slight modifications, but give me some weird results at the end.

Try chomping the lines retrieved first before throwing to while {}, and unlike C/Java you always need the braces for blocks like if-else/for even if the block consists of only one statement. This is likely to work better I think:

open(DATA1,"ping1.txt");
open(DATA2,"ping2.txt");
 
chomp( $data1 = <DATA1>);
chomp( $data2 = <DATA2>);
 
while ($data1 || $data2) {
   if ($data1 ne $data2) {
       print "<$data1\n";
       print ">$data2\n";
   }
 
   chomp($data1= <DATA1>);
   chomp($data2= <DATA2>);
 
 }

Thanks!! That works perfectly.

Yes, I am new to perl..lol similar to C but different at the same time.

:slight_smile:

In shell, you can use diff instead.

diff is so powerful.

relative cmd like : paste, comm, join ....

thanks, I know. I was doing this on a Windows box. I had the MS SFU loaded but it didnt have diff that i could find.

Trust me that was the first thing that came to my mind..lol :slight_smile:

There is a diff for windoze. When I type

diff -v

I get

diff - GNU diffutils version 2.7

wow..lol so there is..

maybe i didnt install it?? or maybe the path is messed up..

anyway. Thanks!! Oh well i guess i needed an excuse to start learning perl anyway since all i have ever used is VBScript :wink:

Sorry; I didn't mean to imply that it comes with windoze. Try this site:

http://sourceforge.net/project/showfiles.php?group\_id=23617&package_id=16423

Instead of Perl, I recommend Ruby.

Here's one way to compare two files using Ruby.

File junk:

acts_as_taggable v2 - Tagging on Steroids for Rails
Dynamic method dispatching with "send"
How to know which interface a broadcast packet comes in on?
Idiomatic conversion of yielding block to array
Lexical Casts with Ruby
MouseHole 1.1 -- rose-colored spectacles for the Web
nano & mega
rpc (not xml-rpc)
traits-0.6.0
Zip Extraction Bug?
EventLoop 0.0.20050825.1600
Intercepting, Resuming Overridden Methods
beginners YAML question
MUD Client (#45)
Thread for scheduling
Beginner: Options with YAML
compiling posgres gem fails on osx
Ruby-specific performance heuristics?
Rake: multiple prerequisites in rule?
template library - specific needs
variables in regex
Word Chains (#44)
Installing a ruby application on a linux system
Still looking for a Ruby MUD client
gmailer-0.0.8 released
Ruby-GetText-Package-1.0.0
Newbie Q: Self-Testing Ruby Files (Double Assignment to Constants)

File junk2:

Installing a ruby application on a linux system
Still looking for a Ruby MUD client
gmailer-0.0.8 released
Ruby-GetText-Package-1.0.0
traits-0.6.0
acts_as_taggable v2 - Tagging on Steroids for Rails
Zip Extraction Bug?
Lexical Casts with Ruby
rpc (not xml-rpc)
Dynamic method dispatching with "send"
MouseHole 1.1 -- rose-colored spectacles for the Web
Idiomatic conversion of yielding block to array
How to know which interface a broadcast packet comes in on?
nano & mega
EventLoop 0.0.20050825.1600
Intercepting, Resuming Overridden Methods
beginners YAML question
Ruby "Game" (Graphics & Sound) Frameworks
Thread for scheduling
Beginner: Options with YAML
compiling posgres gem fails on osx
Ruby-specific performance heuristics?
Rake: multiple prerequisites in rule?
template library - specific needs
variables in regex
Word Chains (#44)

Lines in junk that are not in junk2:
ruby -e 'puts IO.readlines($[0]) - IO.readlines($[1])' junk junk2

MUD Client (#45)
Newbie Q: Self-Testing Ruby Files (Double Assignment to Constants)

Lines in junk2 that are not in junk:
ruby -e 'puts IO.readlines($[0]) - IO.readlines($[1])' junk2 junk

Ruby "Game" (Graphics & Sound) Frameworks

Well actually Diff does come with the Windows SFU package, I just didnt see it on my work box but it was on my home one. Something different i did I am sure. Anyway, thanks for all the help and I will check out Ruby.

hi. im very new to perl and im learning so much through examples in this forums. i have one question about the code above:

-----
open(DATA1,"ping1.txt");
open(DATA2,"ping2.txt");

chomp( $data1 = <DATA1>);
chomp( $data2 = <DATA2>);

while ($data1 || $data2) {
if ($data1 ne $data2) {
print "<$data1\n";
print ">$data2\n";
}

chomp($data1= <DATA1>);
chomp($data2= <DATA2>);

}

---

why is it that chomp($data1=<DATA1>); has to be typed before and after the while statement? thank you very much!

One is in the loop; the other is outside the loop. So that we handle the first entry line and all lines thereafter.

I think the program should work without the chomp(). But I customarily do that for mostly everything I read from <>, so that the end-of-line is removed early on for later processing of the line (yeah there's no special reason except to avoid the newlines sticking around everywhere).