is it possible to write more than one line into file?

Post your questions and help other users.

Moderator: Martin

Post Reply
lord_EarlGray
Posts: 19
Joined: 16 Jun 2018 21:17

is it possible to write more than one line into file?

Post by lord_EarlGray » 09 Jul 2018 17:23

Hi, I'm looking for solution to write more than one line (multiple variables one for each line) to file. I didn't figure out if there is possibility to manage this with script. There is action "Write to File", but for me it seems only to write one line to file.

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: is it possible to write more than one line into file?

Post by Desmanto » 09 Jul 2018 18:21

What are multiple variables example? Do they belong to the same group of data?

List and listformat
The best way to write multiple lines in one go is to use list variable. You can add the variable needed and later when write to file, use the listformat. Example

Code: Select all

a = "The answer to the universe is 42";
b = "1 + 2 = 3";
c = "abcdefghi";

write = newList(a, b, c);
In the write to file action, put this as the text
{write,listformat}
It will create the text as
The answer to the universe is 42
1 + 2 = 3
abcdefghi
Uniform data
Most of the time, the data is uniform, so we can simply just use addElement() instead. Example concept from my battey logger flow. Each periodic check will get some information and add it to a global variable list type (which I have created before)

Code: Select all

row = "{triggertime}\t{battey_level}\t{battery_charger_counter}\t{battery_voltage}\t{battery_current_now}\t{battery_temperature}\t{battery_status}";
addElement(global_battery_log, row);
After adding to the glovar for the whole charging session, I can then export the logged variable from list to a string using listformat in the Write to File Action
{global_battery_log,listformat}

The result is something like this.
1529025661869 1 160233 3574 -1523436 380 2
1529025667857 1 163215 3574 -1991209 380 2
1529025673866 1 166583 3574 -1985838 380 2
1529025679865 1 169920 3574 -1944334 380 2
Concatenate as you go
Other concept can be concatenate the variable as you go. You can start from blank variable and concatenate each one as you continue down the script.

Code: Select all

msg = "";

a = "Hello World";
msg = msg + a + "\n";

b = "This is my first sentence";
msg = msg + b + "\n\n";

c = "===============";
msg = msg + c;
Will show you
Hello World
This is my first sentence

===============
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

lord_EarlGray
Posts: 19
Joined: 16 Jun 2018 21:17

Re: is it possible to write more than one line into file?

Post by lord_EarlGray » 09 Jul 2018 20:40

That works fine, thanks!

Post Reply