How to accept "list" as input from a text file

Hi All,

I need to accept the inputs from a text file, wherein one of the item should be a list. Say as below 2 input data:

Input.txt

item11, item12, ((item13aa,item13ab,item13ac),(item13ba,item13bb,item13bc),…), item14
item21, item22, ((item23aa,item23ab,item23ac),(item23ba,item23bb,item23bc)), item24
and so on…

I’m using separate scripts to input each item of the file and perform the functions. So as a whole we need to repeat all the functions n-times depending on the number of inputs. But the problem i’m facing while inserting the 3rd item with varying number of inputs.

Suggest a method to script for such inputs :?:

You can’t read items from a file directly into a list without first splitting the items using the “split” command. By default SenseTalk can only recognize each item in your file as words, characters, items, and lines. Words are delimited by spaces and items are delimited by commas (,).

If you want to read in a list of items in the form:

item1,item2,item3

…into a list, you first need to read the text into a container. Once you have the values read into a container you will then need to use the SenseTalk split command to split the text into a list of values.


put someText -- displays item1,item2,item3
split someText by "," 
put someText  -- displays (item1,item2,item3)

I would hate to impose a format on your file since it is unclear to me what you need to accomplish. However, I would like to encourage you to experiment with different formats that will make it easier to split groups of text into a list of values as well as retain the readability of the data file itself. Try using a combination of special characters to designate groups of values so you can split them using the “split” command. At the same time, do not get too carried away with these special characters otherwise you will risk the integrity of your file in terms of readability. Moreover, your code needed to parse the file might end up becoming too complicated.

Hi,

I tried applying the split command and work with the inputs, but its not working as per my requirement.

Can you suggest some other way around to accept the inputs from files and work with it based on the following scenario:

Whole application as a whole should iterate depending upon the input lines in a text file. And one particular scenario(say the inputs are taken from one item with multiple data of each input line) and should iterate that many times depending upon the multiple data separately.

Suggest a way to take input for such conditions and script the same.

There are several techniques you can use to access data from a file, including the split command, as Jo suggested. Possibly an even simpler approach is to use the value() function. This function will evaluate any text as a SenseTalk expression, so if your file is structured using standard SenseTalk expressions it will be very easy to read and process.

For example, one way to do this would be to make each line of your file a SenseTalk list, like this:

(1,2,(31,32,33),4,5)
("one","two"("item31","item32","item33"),"item 4","item 5, including a comma")

Then you can process the file like this:

repeat with each line of file "myData"
    set dataList to value(it)
    -- work with the information in dataList
end repeat

To make this work, each line of the file will need to be a list, including the opening and closing parentheses, and it is a good idea to quote your text values as shown on the second line of the example data file above. If your data file is generated from an outside source and doesn’t have parentheses, you can supply them in your script like this:

    set dataList to value( "(" & it & ")" )

This technique can also be used to load information that was previously stored to a file by another script. When creating such a file from a script, it is recommended that you use the standardFormat() function to write the data out, which will properly quote all of the values as needed.

If you continue to have trouble with this, please post a simple script example so we can offer specific suggestions.