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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with IF ELSE statement

Status
Not open for further replies.

nyak

Technical User
Jul 6, 2011
2
IT
Hi to you all

This is my input taken from a txt file:

04001200602151430012000005
04001200602151430000014005

the result I need is:

04001200602151430006006005
04001200602151430007007005

I wrote this code, but the ELSE condition is never "used":

x=substr($i,18,3)
y=substr($i,21,3)
{if(x!=000){y=x=x/2}; else x=y=y/2;}
{print substr($i,1,17) x y substr($i,24,3)}

The result I get is:

04001200602151430006006005
04001200602151430000000005

Why does this happen?


 
Hi

If only one of x or y can be non-zero, then I would let the [tt]if[/tt] out :
Code:
{
  x=substr($0,18,3)
  y=substr($0,21,3)
  printf "%s%03d%03d%s\n",substr($0,1,17),(x+y)/2,(x+y)/2,substr($0,24,3)
}
Regarding your syntax, either :
[ul]
[li][tt]if (x!="000") { y=x=x/2 } else x=y=y/2 [gray]# no semicolon ( ; )[/gray][/tt][/li]
[/ul]
[ul]
[li][tt]if (x!="000") y=x=x/2; else x=y=y/2 [gray]# no braces ( {} )[/gray][/tt][/li]
[/ul]
There you can also see your bug's solution : 000 is numeric 0, not string "000".


Feherke.
 
thank you very much! your help has been very useful!
an '(x+y)/2' is definitely better than an if else statement :)

thanks again!

Paolo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top