Page 1 of 1

operation with files

Posted: 07 Feb 2013 13:27
by inReinbek
Hi programmerteam,

1)
I'm looking for opening / reading a (ascii/txt-)file as one parameter example = {file_en_bloc}
and also I wanted to search in this {file_en_bloc} a special token (like "firstname lastname").

"scriptcodeidea" for user:
file_en_bloc = loadfile (/mnt/sdcard0/datafile.dat)
a = findcontent ({file_en_bloc}, {token})

a has to be "true" or "false"


2)
I want to open/read a file contains a list with phrases
like
phrase1
phrase2
phrase3

"scriptcodeidea" for user:
phrasefile = loadlistfile (/mnt/sdcard0/datafile.dat)
a = countlines {phrasefile}
b = random [1,a]
c = usecontentline b // as text

-----

Any hints / ideas ?

Re: operation with files

Posted: 07 Feb 2013 15:50
by Martin
1) You can read a text file into a variable with the action Init Variable Text File: test.txt to file_text.
There are several functions you can use in a script to process the text.
Following functions might be useful: indexOf, lastIndexOf, length, substring, left, right, startsWith, endsWith or the regex method matches.
The help page of the script action contains a list of all available functions.

For example (in a condition Expression):

Code: Select all

indexOf(file_text, "abc")>=0
The condition will be true when the text abc is found in the text of the file.

2) First read the content of a file into a variable with action Init Variable Text File: test.txt to file_text.
Then use an action Script to split the file into lines and to select a random line:

Code: Select all

//split the text into a list of lines
lines=split(file_text, "\\n");

//pick a random element of the list and store the line in variable s
s=getRandomElement(lines);
You can display the random text using Notification on Screen: Random text is {s}


Regards,
Martin

Re: operation with files

Posted: 08 Feb 2013 10:11
by inReinbek
Hey Martin,

thanks a lot ... great !!!

Regards