Re: compare numbers behaves strange...



di98mase schreef:
On 12 Mar, 15:28, Janis <janis_papanag...@xxxxxxxxxxx> wrote:
On 12 Mrz., 15:15, Luuk <L...@xxxxxxxxxxx> wrote:





di98mase schreef:
Hi
I have a file with numbers in it. My program reads the values and
compare them and depending on the value I increment a counter. what I
have seen is that the evaluation fails. This is an extract of my
numbers:
7
7
7
34
92
111
294
121
142
This is parts of my program that checks the value (ms) read from the
file and increments the counters
if( ms == 18) t18++;
if( ms == 19) t19++;
if( ms >= 20 && ms < 30)
{
print ms;
t20++;
}
if( ms >= 30 && ms < 40) t30++;
if( ms >= 40 && ms < 50) t40++;
if( ms >= 50 && ms < 60) t50++;
if( ms >= 60 && ms < 70) t60++;
As you can see I dont have any numbers between 20 and 30 but I still
get an increment for the t20 counter. When I print the value it is
"294". Why do I have this behaviour?
its compared alphanumeric
The interesting question is; Why?

And the answer might most likely be that 'ms' had been converted
to a string somewhere in an unquoted part of his program (i.e.
the opposite of the conversion you propose below). So if there's
some code like

{ ms = $1 ""
...
if( ms == 18) t18++;
if( ms == 19) t19++;
if( ms >= 20 && ms < 30)
...
}

It might be a more appropriateto fix the original number-to-string
conversion.

Janis





this will work:
if( 0+ms >= 20 && 0+ms < 30)
--
Luuk- Zitierten Text ausblenden -
- Zitierten Text anzeigen -- Dölj citerad text -
- Visa citerad text -- Dölj citerad text -

- Visa citerad text -


Hi all,

this is the missing part that converts from a string to an int that
was not shown in the previous post:

# Try to find out the transaction times from the logs that looks like:
# /I/TR End, Time:82433(89) Stat:0/0016e0dch:(
/TR End, / {

match($3,"Time:")
usecStart = RSTART + RLENGTH
match($3,"[(]")
usecEnd = RSTART
usecLen = usecEnd - usecStart
us = substr($3 , usecStart, usecLen)

# Make sure that all found the matching pattern
if (usecStart != -1 && usecEnd != -1 && usecLen != -1)
{
# Convert to ms and truncate decimals
temp = us/1000;
ms = sprintf ("%d", temp)

if( ms == 1) t1++;
if( ms == 2) t2++;
if( ms == 3) t3++;
if( ms == 4) t4++;
if( ms == 5) t5++;
etc
etc

I thougth that the sprintf would convert the string to a number, or?

man awk says:
A number is converted to a string by using the value of CONVFMT as a format string for sprintf(3), with the numeric value of the variable as the argument.


so, if you want it to be numeric type:
ms = 0 + sprintf ("%d", temp)

but i did not test this to see if your equation will work ;-)

--
Luuk
.



Relevant Pages

  • Re: compare numbers behaves strange...
    ... compare them and depending on the value I increment a counter. ... the opposite of the conversion you propose below). ... Visa citerad text - ...
    (comp.lang.awk)
  • Re: compare numbers behaves strange...
    ... compare them and depending on the value I increment a counter. ... to a string somewhere in an unquoted part of his program (i.e. ... the opposite of the conversion you propose below). ... usecStart = RSTART + RLENGTH ...
    (comp.lang.awk)
  • Re: use a quote for integer data type in SQL statement?
    ... If you compare a string value to an integer value then the string value will ... isn't valid for numeric conversion. ... Specify literal values correctly: ...
    (microsoft.public.sqlserver.programming)
  • Re: compare numbers behaves strange...
    ... compare them and depending on the value I increment a counter. ... This is an extract of my ... the opposite of the conversion you propose below). ...
    (comp.lang.awk)
  • Proposal: String::Format::General
    ... It provides format string parsing and output assembly, you provide the code that implements the individual conversion characters. ... Format syntax is kind of a cross between sprintf and strftime, but how close it is to each of these depends on the semantics implemented by the user. ... Note that the following is pre-alpha documentation; the interface to the output conversion code has changed since yesterday, ... conversion character, and contain a number of optional fields which may ...
    (comp.lang.perl.modules)