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

Extracting parts of a field to process 1

Status
Not open for further replies.

mrr

Technical User
May 3, 2001
67
US
I have a data file with the following info:
111 222 333 -0.3030 30.0050
222 333 444 0.0050 30.0000
333 444 555 -80.9011 -30.0199

I need to index,substring or mathc on the 4th and 5th fields.
I have to convert the data to the left of the decimal into an interger and assign it to a new variable.
I then need to take the data to the right of the decimal and divde by 60 and interger that number and assign to new variable,take the remainder and divide again by 60 and assign the result to another variable.

The fields don't always have the same number of characters to the left or right of the decimal, so I cannot left/right justify and do a column substring extraction.

Thanks for helping on this.

 
Hi mrr,

I'm not sure what you want to do with negative numbers or if I have done the math you want correctly, but this should get you started.

{
for (i=4;i<=5;i++) {
if (substr($i,1,1)==&quot;-&quot;) {
sign = -1
a = substr($i,2)
}
else {
sign = 1
a = $i
}
lft = 0
rgt = 0
j = index(a,&quot;.&quot;)
if (j) {
lft = substr(a,1,j-1)
rgt = substr(a,j+1)
}
n1 = lft*sign
n2 = int(rgt/60)*sign
n3 = int((rgt%60)/60)*sign
print &quot;Line &quot; NR &quot; Field &quot; i &quot; &quot; n1 &quot; &quot; n2 &quot; &quot; n3
}
}

Hope this helps.
CaKiwi
 
Of course, this is a much simpler solution.

{
for (i=4;i<=5;i++) {
n1 = int($i)
rgt = ($i-n1)*10000
n2 = int(rgt/60)
n3 = int((rgt%60)/60)
print &quot;Line &quot; NR &quot; Field &quot; i &quot; &quot; n1 &quot; &quot; n2 &quot; &quot; n3
}
}

It gives a different result for the third field of some negative numbers, but I'll let you decide what you want there.

Let me know if this helps.
CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top