Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If Else problem 1

Status
Not open for further replies.

hazeyb

ISP
Apr 11, 2008
3
GB
Hi,

I've attempted to use if, else in the following;


print "$acrLoopIP\n";
if ($acrLoopIP eq "") {
my $ACRip = $acrLoopIP;
} else {
$ACRblock = new2 Net::Netmask ($acrLoopIP);
my $ACRip = $ACRblock->base();
}

print "$Site, $ACRip\n";

This produces the output;
172.20.0.3/32
Brighton,
172.20.0.3/32
Bristol,

So $acrLoopIP is not "", but $ACRip is not begin calculated.



If I run the same section, without the if else statement it works fine;

print "$acrLoopIP\n";

$ACRblock = new2 Net::Netmask ($acrLoopIP);
my $ACRip = $ACRblock->base();

print "$Site, $ACRip\n";


Output is;

172.20.0.3/32
Brighton, 172.20.0.3
172.20.0.3/32
Bristol, 172.20.0.3

$acrLoopIP is 172.20.0.3/32 and $ACRIP is calcuated as 172.20.0.3

Can anyone point out where I'm going wrong?

Cheers,
 
Hi

hazeyb said:
Can anyone point out where I'm going wrong?
If I give you this sample code :
Perl:
[b]my[/b] [navy]$outervar[/navy][teal]=[/teal][purple]1[/purple][teal];[/teal]

[b]if[/b] [teal]([/teal][purple]1[/purple][teal])[/teal] [teal]{[/teal]
  [b]my[/b] [navy]$innervar[/navy][teal]=[/teal][purple]2[/purple][teal];[/teal]
  [navy]$outervar[/navy][teal]=[/teal][purple]3[/purple][teal];[/teal]
[teal]}[/teal]

[b]print[/b] [green][i]"outervar : ($outervar)\ninnervar : ($innervar)\n"[/i][/green][teal];[/teal]
which produces this :
Code:
outervar : (3)
innervar : ()
will shade light on your mistake ? The reason in one word : scope.

Feherke.
 
Thank you, changed the code to


print "$acrLoopIP\n";
if ($acrLoopIP eq "") {
$ACRip =$acrLoopIP;
} else {
$ACRblock = new2 Net::Netmask ($acrLoopIP);
$ACRip = $ACRblock->base();}

print "$Site, $ACRip\n";


Now produces the output;
172.20.0.3/32
Brighton, 172.20.0.3
172.20.0.3/32
Bristol, 172.20.0.3


which is what I originally expect. Will read up more on scope tomorrow, but you've pointed my in the right direction.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top