Help with addition 
Author Message
 Help with addition

I have a text file that contains various nubmers in it. I am parsing out the
numbers and trying to add them but it seems awk treats them as strings.  I
know I can convert from number to string but can I convert from string to
number. Example of my script below.

Total=substr($0,23,10)+substr($0,55,10)+substr($0,99,10)

Print total

I get a crazy number and all I can think of is that becasue substr() is a
string command I get a string as an answer.

Thanks.



Thu, 01 Jan 2004 00:57:54 GMT  
 Help with addition

Quote:

>I have a text file that contains various nubmers in it. I am parsing out the
>numbers and trying to add them but it seems awk treats them as strings.  I
>know I can convert from number to string but can I convert from string to
>number. Example of my script below.

>Total=substr($0,23,10)+substr($0,55,10)+substr($0,99,10)

>Print total

AWK is not a case-insensitive language.  Try:

print Total

Quote:
>I get a crazy number and all I can think of is that becasue substr() is a
>string command I get a string as an answer.

No, this is entirely correct and it should work.  AWK sees the + operator,
which needs numeric args, so it converts the strings to numbers.


Thu, 01 Jan 2004 01:19:37 GMT  
 Help with addition
Perhaps use int(substr($0,23,10)), or even better, post a few example lines
from your file, together with the associated output you're getting.


Quote:
> I have a text file that contains various nubmers in it. I am parsing out
the
> numbers and trying to add them but it seems awk treats them as strings.  I
> know I can convert from number to string but can I convert from string to
> number. Example of my script below.

> Total=substr($0,23,10)+substr($0,55,10)+substr($0,99,10)

> Print total

> I get a crazy number and all I can think of is that becasue substr() is a
> string command I get a string as an answer.

> Thanks.



Thu, 01 Jan 2004 05:24:42 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Need some help with awk addition

2. 1's complement addition, subtraction help

3. Help: dynamic menu entry additions

4. J FAQ - Repost + additions

5. New Addition to Why Smalltalk 1.7.2003

6. Recent additions Waterloo.

7. Potentially useful TreeView checkbox additions

8. How do the programmers get to the know the additions

9. Rounding error in addition

10. Automatic addition of a lf to a file.

11. Additions to FAQ

12. Fixed point addition problems

 

 
Powered by phpBB® Forum Software