A script to read values from a file

Hi,

I’ve been trying to implement an script to read values from a file and then perform operations with them. I’m able to read the file and place each line into a list.

But now my problem is that I can’t perform operations with those strings. “According to the Apple Script Language Guide” (Apple), strings and real numbers can coerce between them. But doesn’t work for me, I’m not sure what I’m doing wrong. Any help will be greatly appreciate it.

My script looks like:


set contentsM1 to paragraphs of (read file fileM1)
repeat with i from 1 to x
	set val to item i of contentsM1 --'val' will have all the values but I can't perform operations with these values
end repeat

Thank you.

OSX 10.3.2

First, what is ‘x’ in your script?

In your snippet it’s undefined, so you’re not going to be able to use it as a loop variable. Presumably you want it to relate to the number of paragraphs read in, in which case you can:

set x to count contentsM1
repeat with i from 1 to x

Also, nothing in your code snippets relates to coercing strings to numbers. What are you trying to do with the values once they’re read in?

Sorry, i should have include that part of the script code as well, I just pasted the core part before I declared ‘x’, which is the number of values inside each file.

I did not include it because it doesn’t wok, but what I had in mind is something like:


set x to 20 --number of iterations
set sum to 0
set contentsM1 to paragraphs of (read file fileM1)

repeat with i from 1 to x
	set val to item i of contentsM1
	set sum to sum + val -- I've edited this, Imade a mistake and was posted before as: set sum to sum + item, guess I need to sleep 
end repeat

set avg to sum / x --average of those numbers

The numbers in the file are in a format like:

0.12345678
0.87654321

Thank you.

I’ve notice the following:

When I read from the file I have a list like the following in the ‘Result’ window:

{“0.0139222”, “0.0380849”, … , “0.016047”}

Twenty elements in a list. And as I said in my previos post I can’t use them as numbers. On the other hand, if I insert in the script a list like the previos one and I use it, instead of the one I’m reading from the file, it works! :shock:

set contentsM1 to {"0.01399222", "0.0380849", ... , "0.016047"}

Another thing is that, when I tried to paste the contents of the ‘Result’ window in the ‘post’ window into the browser (here) and I do a preview, it doesn’t show what I’ve pasted from there.

I guess all this has to do with the format in which the file is being read or something.

Any help is appreciate it.

Two things, a) try coercing the item to a real, and b) perhaps you’d be better off using the exact number of paragraphs of the file in case there are more or less entries than 20. You could further modify it so that if the total entries were less than 20 it used those and if it was more, it limited it to 20:

set sum to 0
set contentsM1 to paragraphs of (read file fileM1)
set x to (count of contentsM1)
if x > 20 then set x to 20 --comment this out if you want to use all the values

repeat with i from 1 to x
	set sum to sum + ((item i of contentsM1) as real)
end repeat

set avg to (sum / x)

Jon

Solved, as I stated before I was guessing it was a problem related with the file.

The problem was that the file was created before, also with an script, taking values from the “Result” window in the Script Editor (not sure if this is the correct way to say it, but I hope you get my point). Anyway, the way I was inserting the values in to the file was without specifying the type:

set R1 to result

when I should have said:

set R1 to result as string

I notice that something was wrong because of in a text editor I could notice some kind of double spaces between the numbers (but spaces without space, I know it has no sense). Nevermind, now that I have defined the type before inserting the values into the file works great.

I appreciate your help Jon. Your code to write into a file has been very helpful as an example to accomplish what I wanted to do, and certainly that way to count the items in the list is more efficient. I’m almost there. :smiley:

Thank you all, Max. :smiley: