Ignoring values less than on a read command

Hi Guys,

I’m working with csv files that record 3 attributes of a frequency analysis task. The attributes are time, frequency Hz and confidence (confidence is the level of certainty from 0.0-1.0 that the frequency estimation is accurate).

I have a piece of code here that reads the CSV, storing the values of every row of each column (time=x, frequency=y and confidence=z.) into the variable csvData.


set my_data to read recCsv
set csvData to paragraphs 2 thru -2 of my_data as list -- paragraph 1 is just the attribute names

set freqData to {}

set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","

repeat with a_reading in csvData
	try 
	set x to text item 1 of a_reading -- timestamp from CSV
	set y to text item 2 of a_reading -- frequency readings from CSV
	set z to text item 3 of a_reading -- confidence readings from CSV
	set end of freqData to y
	end try
end repeat

set A to 0

repeat with this_freq in freqData
	set A to A + this_freq
end repeat ...

And a short snippet of the CSV readings:

time,frequency,confidence
0.740,46.899,0.401016
0.750,45.635,0.103832
0.760,45.540,0.097100
0.770,45.353,0.071827
0.780,47.367,0.065402
0.790,51.383,0.105370
0.800,55.067,0.160389
0.810,57.213,0.331173
0.820,57.078,0.527134
0.830,56.089,0.696078
0.840,55.007,0.667735
0.850,53.657,0.622470
0.860,52.439,0.739081

I would like to know how to adapt the code so that any row that has a confidence reading lower than 0.6 is disregarded and no information from that row be stored in the variable csvData (if thats possible). I’ve been searching online for info but haven’t found anything thats helped.

Any help would be much aopreciated.

Doug

Model: 2019 2.9 or something xeon core 16gb
AppleScript: Script Debugger 8
Browser: Safari 605.1.15
Operating System: macOS 10.15

repeat with a_reading in csvData
	set z to text item 3 of a_reading -- frequency readings from CSV
	if z > "0.6" then -- CAUTION: it's text, not a number
		
		-- do your other stuff here
		
	end if
end repeat

Hi. This shell approach will read, separate, and value:

set csvData to do shell script "awk -F ','  '$3>=0.6' " & (choose file)'s POSIX path's quoted form

Thank you very much for replying.

Sorry for not being clear. I guessed the AS command would start with if z > .6. Its how to ignore the entire row i’m stuck on.

Whilst the shell script is doing the job, I would be nice to know how the AS command would go, If anyone has time to respond thatd be great.

Thanks for your help!

Put what’s inside your try statement (i.e. set x,y,z) where it says ‘do your other stuff here’. Then, the variables will only be set when the confidence exceeds .6. You could probably lose the try statements as well.

FWIW, I’d probably combine the two lines into a single one, which would avoid setting z twice:

[format]if text item 3 of a_reading is greater than or equal to “0.6” then – CAUTION: it’s text, not a number[/format]

Oh sorry, such a berk, totally misread that. But thats not working either, I took a wild guess. this removes rows but the results do not add up

if z > "0.6" then set the row's contents to null

This kind of thing has been happening intermittently recently, my machine outputting incorrect values or not recognising values/commands correctly. i upgraded to Big Sur then d/graded back to catalina on the same apfs container and i think theres a format issue. Feel nauseated by the thought of a clean install it takes me about a week to install all the audio 3rd party stuff.

Perhaps its time to face the bleak, bleak music