Angus am Freitag, 3. Februar 2006 10.13:
> Joe,
>
> Thank you for taking the time to explain this bit of code. I have spent
> some time trying to understand the ternary operator (?:) this evening and I
> think it is making more sense. In the past I have seen this operator and
> moved on in favor of using if/then/else statements but I can see how in
> this example it is much simpler to evaluate the EXPR with a ternary than to
> use code blocks. I can now see mostly how this expression works in your
> example. I see that the first expression says $actual{$_} equal
> $register{$_} if this is true then $_ equals the hostname.
gotcha :-)
> But, if this
> statement is false then I see that we dereference the $_ values in both
> Arrays.
Not quite shure what you mean here
> When I do run this code though it seems to print out the memory address for
> the hostname reference which I think means it is not being dereferenced
> correctly but I don't see how it differs form the else part of the print
> statement
>
> ARRAY(0x816e00c) differs:actual 164.72.119.175 <-> register 164.72.119.179
> ARRAY(0x816e03c) differs:actual 164.72.123.43 <-> register 164.72.21.43
> host3 is ok
> ARRAY(0x819bed4) differs:actual 164.72.98.89 <-> register 164.72.8.89
> host5 is ok
Hm, indeed (now after testing) - it is my fault, but it motivated you to go
into the details :-)
I posted a correction to my first version, and I did not consider all side
effects of the faulty first version.
Further, my code misses an information: The host name in the case the two IPs
differ.
I'll post below the code in the first version (with the mentioned missing
info), it's runnable now :-)
Then, some snippets for a modification follows to get the host name in the
result also in the case of differing IPs.
=== BEGIN first version ===
#!/usr/bin/perl
use strict;
use warnings;
my %actual = (
"host1" => "164.72.119.175",
"host2" => "164.72.123.43",
"host3" => "164.72.45.98",
);
my %register = (
"host1" => "164.72.119.179",
"host2" => "164.72.21.43",
"host3" => "164.72.45.98",
);
my @res = map {
( $actual{$_} eq $register{$_} )
? $_
: [ $actual{$_}, $register{$_} ] ### (A) ###
} sort keys %register;
foreach my $e (@res) {
if (!ref($e)) {
print "$e is ok\n";
}
else {
# corrected the following print, since host name is not in @res in
# this case ### (B) ###
print "Differing IPs (host info missing): ",
"actual @{[$e->[0]]} <-> register @{[$e->[1]]}\n";
}
}
=== END first version ===
Ok, now to get the host name into the result, there are several possibilities.
The easiest way is to modify the (A) and (B) lines above like:
: [ $_, $actual{$_}, $register{$_} ] # host name, IP 1, IP 2
print "host @{[$e->[0]]} differs: ",
"actual @{[$e->[1]]} <-> register @{[$e->[2]]}\n";
# note that the indexes have changed.
> I still have some ways to go with really understanding this but I am
> closer.
Yes, I can see that! Good luck!
> P.s. Your English is probably better than mine; I wouldn't know it wasn't
> your first language if you didn't mention it.
Thanks :-)
(learning english is the main reason for me to be on the list)
joe
[whole history snipped away]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>