How to select the highest value.

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
maverickalex
Posts: 108
Joined: Sun Mar 15, 2009 11:51 pm

How to select the highest value.

Post by maverickalex » Mon Apr 04, 2011 11:00 am

Hi,

I have a file that my app grabs info from.

The file is newline delimited and has just 6 lines. however these are updated every second, so the same lines are continued thousands of times.

An excerpt.

Code: Select all

AVA001
EDDF
EDDB
fuel 19717.886719
240.741669
CRJ-900
AVA001
EDDF
EDDB
fuel 19716.490234
241.675003
CRJ-900
AVA001
EDDF
EDDB
fuel 19715.078125
242.658340
CRJ-900
AVA001
EDDF
EDDB
fuel 19713.728516
243.591660
CRJ-900
As you can see some of the lines do not change throughout and are just strings, others are float values and change each cycle.
The fuel figure is the one i have issues with.

I'll explain the issue and perhaps the brains here could help me with my issue.
previously to get a total fuel used figure i did this.

Code: Select all

put char 1 to 10 of line 4 of url("file:" & tFullPath)into field "fuelstart"
      put char 1 to 10 of line -3 of url("file:" & tFullPath)into field "fuelend"
         -- calculate fuel used
      set the numberformat to "0000.00"
      put the round of field "fuelend" - field "fuelstart" into field "fuelused"
(i never had the word fuel before the figure previously)

this grabs the first figure in fuel, subtracts the last giving me a total.

It works well this way.

However when deep testing it was pointed out to me that the user of the simulator is able to adjust the starting fuel figure, also if the aircraft is not started the fuel value is zero, which my app does not take into consideration.

so in the example text file above the fuel figure would start at 19717 (the figure my script grabs)
but the sim user could raise this figure (which would be captured in the text file at some point further down the list in the text file).

i included the word fuel in the new protocol so i could try and use a script that would identify the lines of text concerning fuel values.
Can anyone assist in what type of script could help?

I'm thinking of putting all the lines which include "fuel" into tfuledata
then getting the highest value in tfueldata into tfuelstart
tfuelstart into field fuelstart?

something like that,

Thanks
Alex

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: How to select the highest value.

Post by Dixie » Mon Apr 04, 2011 11:41 am

Alex...

I'm not sure that I fully understand... but here goes.

From the data you have supplied, I am assuming that line 4 is the fuel that you start with and line 5, for example, is the fuel that is used... You now want the 'start fuel' (line 4) subtracted from the 'fuel used' ( line 5) to give you what is left in the tank, as it were ?

Code: Select all

on mouseUp
   put empty into fld 2
   
   put 1 into count
   repeat for the number of lines of fld 1
      if word 1 of line count of fld 1 = "fuel" then
         put (word 2 of line count of fld 1) - (word 1 of line (count +1) of fld 1) & cr after tFuelData
      end if
      add 1 to count
   end repeat
   
   --sort lines numeric descending of tFuelData
   put tFuelData into fld 2
end mouseUp
Hope it helps

Dixie

maverickalex
Posts: 108
Joined: Sun Mar 15, 2009 11:51 pm

Re: How to select the highest value.

Post by maverickalex » Mon Apr 04, 2011 12:13 pm

Hi Dixie, my apolgies. the post isnt overly clear.

The fuel line (line 4) is the only line which contains the fuel data in the six line chunk. the six values you see are repeated every second, so in the example the next fuel figure is line 10 then line 16.

the other values are not connected.

in short when the simulator (from where i grab the data) starts.

1. aircraft can be sat engine off. therefore the fuel figure would read 0.
2. It could be started, the engine be on and have a fuel figure of 25000.
3. it could be started, the engine be on and the user alter the fuel to a different figure.

So my app falls down as it presently grabs the first line of fuel from the txt file (line 4) and for the end fuel figure would be the third to last line (-3) of the text file.
this would work fine for example 2 as line 4 would read 25000 and the value would not get higher. therefore the script in my first post which calculates the first line of fuel - the last line of fuel to get the total works.

But example 1 and 3 returns incorrect values.
What i want is to search all the lines of fuel to find the highest figure, this would be used as the fuelstart value.

I hope that makes sense.

Alex

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: How to select the highest value.

Post by dunbarx » Mon Apr 04, 2011 6:13 pm

Try either of these. I put the data in a field "input". But more importantly, step through each and see how they work and what you get in the variable watcher.

Code: Select all

on mouseup
   get fld "input"
   repeat for each line tline in it
      if tLine contains "fuel" then put tline & return after temp
   end repeat
   sort temp descending by word 2 of each
   answer line 1 of temp
end mouseup

on mouseup
   get fld "input"
   repeat with y = 1 to the number of lines of it
      if line y of it  contains "fuel" then put "Line" & y && ":" & line y of it  & return after temp
   end repeat
   sort temp descending by the last word of each
   answer line 1 of temp
end mouseup

maverickalex
Posts: 108
Joined: Sun Mar 15, 2009 11:51 pm

Re: How to select the highest value.

Post by maverickalex » Tue Apr 05, 2011 9:33 am

Thanks Dunbarx, i used the first example and adjusted it to put the txt file into the variable.

Thanks again for your help.

Alex

Post Reply