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?
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 "<$data1";
print ">$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
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>);
}
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
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.
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).