Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
PUBLIC CLOUD
PRIVATE CLOUD
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Web CMS (TeamSite)
difference in results of else and elsif
Mevan
Hi,
I have a if condition in my perl file. Its something like,
if(server eq "windowsXp"){}
elsif(server eq "Windows7" || server eq "Solaris10"){}
This worked. But did not give me a proper output as I expected. It started skipping values. I tried it in the way below, and it gave me the results I needed.
if(server eq "windowsXp"){}
else{}
I am just curious to know why this happened. My else is always going to have Windows7 or Solaris10. So what made it behave this way?
Thanks.
Find more posts tagged with
Comments
Migrateduser
This looks like some really incomplete code. Where's your $ in front of the variable. Where is it initially declared and initialized? Can't help you much until we see more of the code.
Mevan
Following is the code which skips the $server values in the elsif part. If I replace elsif with else, it works fine.
foreach $server (
@nameArr)
{
if($server eq "windowsXp"){
-- some script here --
}
elsif($server eq "Windows7" || $server eq "Solaris10"){
}
}
Migrateduser
Of course else works. Else will always work if the value does not match the first if condition (unless the value is not initialized, then you will get an error)
I can tell you are not normally a developer. If you were, you would have known that. You should really invest in some training.
Migrateduser
Print out this: "###$server###". I'm betting you have a space or a newline in your string. Unless the value printed is ###Windows7### or ###Solaris10###, you will need to clean up your value.
Migrateduser
The other thing to note is that the comparison on the if statement is case sensitive.
windowsXp is not the same as windowsXP, or WindowsXP.
The following will convert to lower case and trim your $server variable
if (trim(lc($server)) eq "windowsxp"){
}
Mevan
I'm betting you have a space or a newline in your string.
You are right. It is a space issue. I just tried to merge two values and check them in elsif loop. Something like (eg: windows . "_" . 7.), which had a space in between.
Thank you!