On 12 Mrz., 15:33, di98mase <di98m...@[EMAIL PROTECTED]
> wrote:
> On 12 Mar, 15:28, Janis <janis_papanag...@[EMAIL PROTECTED]
> wrote:
>
>
>
>
>
> > On 12 Mrz., 15:15, Luuk <L...@[EMAIL PROTECTED]
> 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
>
> > > > =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?
>
> > > 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
>
> > =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
>
> > > this will work:
> > > if( 0+ms >=3D 20 && 0+ms < 30)
>
> > > --
> > > Luuk- Zitierten Text ausblenden -
>
> > > - Zitierten Text anzeigen -- D=F6lj citerad text -
>
> > - 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, / {
>
> =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


|