For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2005 > Beginner question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Beginner question
Kyle Lampe

2005-03-26, 8:56 am

Hello!

I've got a quick perl question for you. I'm writing a script that
compares two xml files, ignoring one certain tag.

The problem I have is I want to get the output from two files simultaneously.

What I'm trying is:

while ( $lineFile1 = <XMLFILE1> && $lineFile2 = <XMLFILE2> ) {
if ( $lineFile1 =~ /$IGNOREXML/ ) {
print "Ignoring: ", $lineFile1;
}
else {
if ($lineFile1 eq $lineFile2) {
print "Same: ", $lineFile1;
}
else {
print("Not the same\n");
print($lineFile1);
print("\n");
print($lineFile2);
exit;
}
}
}

But it's complaining about my &&. How can I increment the contents of
2 files like this at once?

Thanks!

_Kyle
David Kirol

2005-03-26, 3:55 pm

Kyle Lampe wrote:
> Hello!

Hi
>
> I've got a quick perl question for you. I'm writing a script that
> compares two xml files, ignoring one certain tag.
>
> The problem I have is I want to get the output from two files simultaneously.
>
> What I'm trying is:
>
> while ( $lineFile1 = <XMLFILE1> && $lineFile2 = <XMLFILE2> ) {

try:
while ( $lineFile1 = <XMLFILE1>, $lineFile2 = <XMLFILE2> ) {
> if ( $lineFile1 =~ /$IGNOREXML/ ) {
> print "Ignoring: ", $lineFile1;
> }
> else {
> if ($lineFile1 eq $lineFile2) {
> print "Same: ", $lineFile1;
> }
> else {
> print("Not the same\n");
> print($lineFile1);
> print("\n");
> print($lineFile2);
> exit;
> }
> }
> }
>
> But it's complaining about my &&. How can I increment the contents of
> 2 files like this at once?

Why the logical and?
>
> Thanks!
>
> _Kyle

Offer Kaye

2005-03-27, 8:55 am

On Fri, 25 Mar 2005 14:07:02 -0800, Kyle Lampe wrote:
>
> while ( $lineFile1 = <XMLFILE1> && $lineFile2 = <XMLFILE2> ) {


[...snip...]

> But it's complaining about my &&. How can I increment the contents of
> 2 files like this at once?
>


According to "perldoc perlop", the && operator has higher precedence
than the assignment operator, "=". So what you really wrote is:
while ( $lineFile1 = (<XMLFILE1> && $lineFile2) = <XMLFILE2> ) {
which is of course not what you meant. Always check operator precedence!
The solution is to either use "and", or use parenthesis to spell out
exactly what you want. In any case, if running under "use warnings"
(or "-w"), you would get a warning about this construct if you don't
use "defined", so it is best if you also added that also:
while ( defined($lineFile1 = <XMLFILE1> ) && defined($lineFile2 =
<XMLFILE2> ) ) {

Needless to say, if you're not running under "use strict;" and "use
warnings;" you're doing a very bad thing... ;-)

Hope this helps,
--
Offer Kaye
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com