ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The following code snippet it that is hanging me up.
The value of c and b are equal numerically. But with the operation I am doing with c the value of c is implicity changing to type string and the comparision is then for string not for numeric values. (My understanding)
Code:
BEGIN {
a = 0
b = 0
c = 0
c=sprintf("%5.2f",b)
gsub(" ","0",c)
gsub("[.]","",c)
print "c = "c
print "b = "b
if ( c != b )
{
print "b and c are not equal"
}
else
{
print "c and b are equal"
}
}
And if I force c to be numeric before comparison
by
Code:
c+=0
The comparison becomes numeric but the value of c is trimmed to a single 0.
Is there any way to cope with this. i.e comparison to be numeric without typecasting.
Gawk performs comparisons as follows: If two variables are
numeric, they are compared numerically. If one value is
numeric and the other has a string value that is a
``numeric string,'' then comparisons are also done numeri-
cally. Otherwise, the numeric value is converted to a
string and a string comparison is performed. Two strings
are compared, of course, as strings. According to the
POSIX standard, even if two strings are numeric strings, a
numeric comparison is performed. However, this is clearly
incorrect, and gawk does not do this.
No, unable to figure out what version is there. Hopefully it is the one that comes with HP UX box
hmmm... so this does not work:
Code:
gawk --version
And I think I can make sense of the test not working.
When you do the print prior to the test of 'c' it is four (4) zeroes (0): c = 0000
You have converted this to a string so you can see the padding and therefore it is now a string.
The bit that you think should work is:
Quote:
If one value is numeric and the other has a string value that is a ``numeric string,''
The problem with this is that the string "0000" is not numeric where as "0" is.
As a proof to this I changed the code:
Code:
BEGIN {
a = 0
b = 0
c = 0
c=sprintf("%5.2f",b)
gsub(" ","",c)
gsub("[.]00","",c) #now c is just equal to a single 0
print "c = "c
print "b = "b
if ( c != b )
print "b and c are not equal"
else
print "c and b are equal"
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.