For Programmers: Free Programming Magazines  


Home > Archive > AWK > March 2008 > compare numbers behaves strange...









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author compare numbers behaves strange...
di98mase

2008-03-12, 7:01 pm

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?

Ben

2008-03-12, 7:01 pm

On 12 mar, 14:40, di98mase <di98m...@hotmail.com> wrote:
> 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
>
> =A0 =A0 =A0 if( ms =3D=3D 18) t18++;
> =A0 =A0 =A0 if( ms =3D=3D 19) t19++;
> =A0 =A0 =A0 if( ms >=3D 20 && ms < 30)
> =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0print ms;
> =A0 =A0 =A0 =A0 =A0t20++;
> =A0 =A0 =A0 }
> =A0 =A0 =A0 if( ms >=3D 30 && ms < 40) t30++;
> =A0 =A0 =A0 if( ms >=3D 40 && ms < 50) t40++;
> =A0 =A0 =A0 if( ms >=3D 50 && ms < 60) t50++;
> =A0 =A0 =A0 if( ms >=3D 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?


You should probably do like this :

ms2 =3D int(ms/10)
Tab[ms2]++

# Having the result
END {
for ( x in Tab ) {
print x "\t" Tab[x]

}
Luuk

2008-03-12, 7:01 pm

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

this will work:
if( 0+ms >= 20 && 0+ms < 30)



--
Luuk
Janis

2008-03-12, 7:01 pm

On 12 Mrz., 15:15, Luuk <L...@invalid.lan> wrote:
> di98mase schreef:
>
>
>
>
>
>
>
>
>
>
>
> 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 =3D $1 ""
...
if( ms =3D=3D 18) t18++;
if( ms =3D=3D 19) t19++;
if( ms >=3D 20 && ms < 30)
...
}

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

Janis

>
> this will work:
> if( 0+ms >=3D 20 && 0+ms < 30)
>
> --
> Luuk- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -


di98mase

2008-03-12, 7:01 pm

On 12 Mar, 15:28, Janis <janis_papanag...@hotmail.com> wrote:
> On 12 Mrz., 15:15, Luuk <L...@invalid.lan> wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
> 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
>
> =A0 =A0{ =A0ms =3D $1 ""
> =A0 =A0 =A0 ...
> =A0 =A0 =A0 if( ms =3D=3D 18) t18++;
> =A0 =A0 =A0 if( ms =3D=3D 19) t19++;
> =A0 =A0 =A0 if( ms >=3D 20 && ms < 30)
> =A0 =A0 =A0 ...
> =A0 =A0}
>
> It might be a more appropriateto fix the original number-to-string
> conversion.
>
> Janis
>
>
>
>
>
>
>
>
> - Visa citerad text -- D=F6lj 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 =3D RSTART + RLENGTH
match($3,"[(]")
usecEnd =3D RSTART
usecLen =3D usecEnd - usecStart
us =3D substr($3 , usecStart, usecLen)

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

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

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

2008-03-12, 7:01 pm

di98mase schreef:
> On 12 Mar, 15:28, Janis <janis_papanag...@hotmail.com> wrote:
>
>
> 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
di98mase

2008-03-12, 7:01 pm

On 12 Mar, 15:51, Luuk <L...@invalid.lan> wrote:
> di98mase schreef:
>
>
>
>
>
>
[color=darkred]
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> man awk says:
> A number is converted to a string by using the value of CONVFMT as =A0a
> format =A0string =A0for =A0sprintf(3), =A0with =A0the =A0numeric =A0value =

=A0of =A0the
> variable as the argument.
>
> so, if you want it to be numeric type:
> ms =3D 0 + sprintf ("%d", temp)
>
> but i did not test this to see if your equation will work ;-)
>
> --
> Luuk- D=F6lj citerad text -
>
> - Visa citerad text -


Ok, thanks Luuk . I am not sure if I understand the bit from the
manual but I will look it up and read about it.

I tested the solution to add 0+ as you suggested before and it worked
so I guess that this will work as well.

Thx

/di98mase
Janis

2008-03-12, 7:01 pm

On 12 Mrz., 15:33, di98mase <di98m...@hotmail.com> wrote:
> On 12 Mar, 15:28, Janis <janis_papanag...@hotmail.com> wrote:
>
>
>
>
>
>
>
>
I[color=darkred]
>
>
[color=darkred]
>
>
[color=darkred]
>
>
>
>
>
>
>
>
>
>
>
>
> 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, / {
>
> =A0 =A0match($3,"Time:")
> =A0 =A0usecStart =3D RSTART + RLENGTH
> =A0 =A0match($3,"[(]")
> =A0 =A0usecEnd =3D RSTART
> =A0 =A0usecLen =3D usecEnd - usecStart
> =A0 =A0us =3D substr($3 , usecStart, usecLen)
>
> =A0 =A0# Make sure that all found the matching pattern
> =A0 =A0if (usecStart !=3D -1 && usecEnd !=3D -1 && usecLen !=3D -1)
> =A0 =A0{
> =A0 =A0 =A0 # Convert to ms and truncate decimals
> =A0 =A0 =A0 temp =3D us/1000;
> =A0 =A0 =A0 ms =3D sprintf ("%d", temp)
>
> =A0 =A0 =A0 if( ms =3D=3D 1) t1++;
> =A0 =A0 =A0 if( ms =3D=3D 2) t2++;
> =A0 =A0 =A0 if( ms =3D=3D 3) t3++;
> =A0 =A0 =A0 if( ms =3D=3D 4) t4++;
> =A0 =A0 =A0 if( ms =3D=3D 5) t5++;
> etc
> etc
>
> I thougth that the sprintf would convert the string to a number, or?


No. All the printf, sprintf, etc. functions/commands format arguments
to (mainly) be displayed to a human (or output to a text interface).

> # Convert to ms and truncate decimals
> temp =3D us/1000;
> ms =3D sprintf ("%d", temp)


In case you want to change the number for internal calculation (or
comparison) use int() for truncation...

temp =3D us/1000
ms =3D int (temp)


Janis
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com