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

Calculations Involving Two Rows 2

Status
Not open for further replies.

thunderkid

Technical User
Oct 26, 2000
54
US
I am trying to make a calculation from data below for equipment simulation.Sample of data is given below for short and med cases. The calculation is for solvent that is expended during operation. Col 6 shows the amount of solvent available. To get expended solvent you have to
subtract next row from previous row for col 6. Whenever there is an 8 incol 6 it means that equipment has been refilled, thus starting with full tank.

short 1 1 455 17 8
short 1 1 455 20 6
short 1 1 455 20 4
short 1 1 345 20 4
short 1 2 405 17 8
short 1 2 220 17 6
med 1 1 455 17 8
med 1 1 450 20 6
med 1 1 425 20 6
med 1 1 420 20 4
med 1 1 415 20 0
med 1 2 410 17 8
med 1 2 415 20 6
med 1 1 410 20 6
med 1 2 350 17 6
med 1 2 315 20 4
med 1 2 310 20 2

Desired results
short 1 1 455 17 8 0
short 1 1 455 20 6 2
short 1 1 455 20 4 2
short 1 1 345 20 4 0
short 1 2 405 17 8 0
short 1 2 220 17 6 2
med 1 1 455 17 8 0
med 1 1 450 20 6 2
med 1 1 425 20 6 0
med 1 1 420 20 4 2
med 1 1 415 20 0 4
med 1 2 410 17 8 0
med 1 2 415 20 6 2
med 1 1 410 20 6 0
med 1 2 350 17 6 0
med 1 2 315 20 4 2
med 1 2 310 20 2 2

I have tried the following coding, but have only been
able to calculate the difference between 8 and col 6 values
and add this value to new col 7. This is incorrect.

{solv = 8 - $6}
{ print $0, solv
}

Any suggestions or assistance is appreciated.

thunderkid



 
Code:
{
    print $0, $6==8? 0: prev - $6
    prev = $6
}
 
mikevh,
Your answer worked great. I have seen the use of ":" and "?". Could yo explain how it works?
Thanks.

thunderkid
 
?: is like an if-then-else, "if expr1 then expr2 else expr3," with ? standing in for then and : for else.
To illustrate, the code could be written like this (using an extra variable):
Code:
{
    if ($6==8) x = 0; else x = prev - $6
    print $0, x
    prev = $6
}


 
mikevh
Double thanks for solution and follow-up explanation. Have a star!

thunderkid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top