operation with files

Post your questions and help other users.

Moderator: Martin

Post Reply
inReinbek
Posts: 95
Joined: 02 Feb 2013 22:04

operation with files

Post by inReinbek » 07 Feb 2013 13:27

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 ?

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: operation with files

Post by Martin » 07 Feb 2013 15:50

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

inReinbek
Posts: 95
Joined: 02 Feb 2013 22:04

Re: operation with files

Post by inReinbek » 08 Feb 2013 10:11

Hey Martin,

thanks a lot ... great !!!

Regards

Post Reply