[SOLVED] How to get a value from key?

General discussions about Automagic and automation in general

Moderator: Martin

Post Reply
tetsugami
Posts: 7
Joined: 30 Jul 2017 15:59

[SOLVED] How to get a value from key?

Post by tetsugami » 31 Jul 2017 10:54

Hy everyone, i want to store and get a value from a file. I dont have a problem when i store a value to file. But when i try get the value i got a problem

For example i stored a value to Surti.txt file:
Titlename=sir
Age=21

And when i get the value the output is
Hello titlename=sir.
Your age is Age=21 years old

I want the output is:
Hello sir.
Your age is 21 years old.

And when i get the value i want to check the key, for example:
If i make a variable titlename the value is titlename key value. And if i make a variable age the value is age key value. A variable is loaded from Surti.txt file.

Do you have an idea to do this??
I had attach my flow to explain that
Attachments
flow_Flow2_20170731_173946.xml
(8.51 KiB) Downloaded 839 times
Last edited by tetsugami on 03 Aug 2017 01:49, edited 1 time in total.

tetsugami
Posts: 7
Joined: 30 Jul 2017 15:59

Re: [ASK] How to get a value from key?

Post by tetsugami » 31 Jul 2017 11:02

Sorry i attach the wrong file here is it..
Attachments
flow_Surti_Assistant_20170731_180049.xml
(8.56 KiB) Downloaded 813 times

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

Re: [ASK] How to get a value from key?

Post by Desmanto » 31 Jul 2017 13:04

First of all, I change a bit your code, for the getElement(). We can easily access element by using []. I will use this onward.
titlename = list[0] is the same as titlename=getElement(list, 0)

When you saved, you should retrieve it in the same format. When you save it, you save as

Code: Select all

titlename={titlename}
umur={umur}
That will give you
titlename=sir
umur=21

When you retrieve it, it becomes
listt=split(file_text, "\\n"); list=["titlename=sir","umur=21"]
titlename=getElement(list, 0); titlename="titlename=sir"
umur=getElement(list, 1); umur="umur=21"

As you can see, the "titlename=" is still there. If you want to get the "sir" only, you should split it again using "=". Then the "sir" will be on index 1 (second element).
x=split(list[0], "\\="); split the first line by "="
titlename = x[1]; access the "sir"

y=split(list[1], "\\="); split the second line by "="
umur = y[1]; access the "21"

Then you can use the {titlename} and {umur} anywhere you want.

If the data is only two lines, I prefer just save the data without any tag in front of them. Just save it as
sir
21


Then you only need to split once. To access the "sir", just use list[0] and the "21" just use list[1].

For the TTS, there is Indonesian offline voice from nuance too (Damayanti). You can try that one. Feel free to visit kaskus as well.
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.

tetsugami
Posts: 7
Joined: 30 Jul 2017 15:59

Re: [ASK] How to get a value from key?

Post by tetsugami » 02 Aug 2017 23:37

Desmanto wrote:First of all, I change a bit your code, for the getElement(). We can easily access element by using []. I will use this onward.
titlename = list[0] is the same as titlename=getElement(list, 0)

When you saved, you should retrieve it in the same format. When you save it, you save as

Code: Select all

titlename={titlename}
umur={umur}
That will give you
titlename=sir
umur=21

When you retrieve it, it becomes
listt=split(file_text, "\\n"); list=["titlename=sir","umur=21"]
titlename=getElement(list, 0); titlename="titlename=sir"
umur=getElement(list, 1); umur="umur=21"

As you can see, the "titlename=" is still there. If you want to get the "sir" only, you should split it again using "=". Then the "sir" will be on index 1 (second element).
x=split(list[0], "\\="); split the first line by "="
titlename = x[1]; access the "sir"

y=split(list[1], "\\="); split the second line by "="
umur = y[1]; access the "21"

Then you can use the {titlename} and {umur} anywhere you want.

If the data is only two lines, I prefer just save the data without any tag in front of them. Just save it as
sir
21


Then you only need to split once. To access the "sir", just use list[0] and the "21" just use list[1].

For the TTS, there is Indonesian offline voice from nuance too (Damayanti). You can try that one. Feel free to visit kaskus as well.
Thanks for answering Desmanto, if I have more variables stored in the file, it also needs a lot of variables x and y, can i just use a substr command to get a value from the key or use a searching in the list code??

I think this app use a java script maybe i can use a searching code in the list. Can i use custom function in this program?

Or can i use a map function?? I see in Script about map,
Ithunk i can use this but how i generate map from list or file

tetsugami
Posts: 7
Joined: 30 Jul 2017 15:59

[SOLVED] How to get a value from key?

Post by tetsugami » 03 Aug 2017 01:48

Yes, i successfull run this script i modify the script like

Code: Select all

list=split(file_text, "\\n");
map=newMap();
for (i in list)
{
 x=split(i, "=");
 map[x[0]]=x[1];
}

titlename=map["titlename"];
umur=map["umur"];

And the output:

Code: Select all

Hello sir
Your age 21
Thanks before for answer my question. :))

Maybee i will discus about automagic in kaskus. I attach the file
Attachments
flow_Surti_Assistant_20170803_084310.xml
(8.73 KiB) Downloaded 817 times

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

Re: [SOLVED] How to get a value from key?

Post by Desmanto » 03 Aug 2017 05:20

Don't know what is the base language for automagic, but i ever read somewhere for the regex part, it is based on java. What is the custom function that you need? You can try to see the Script documentation to check whether there is the function you need. Or you can just simply tap "function" at the script and find the keyword you want. To search, you may use keyword "find" or "search"; there will be some functions you probably need there. There are function substring(), contains() to search within a variable findAll() to find using regex, or indexOf() to search and return the index of search.

Example, to use substring to take the "sir" from "titlename=sir", you can use substring("titlename=sir", 10). Depends on your scripting style, you may use another way to achieve the same thing. (which you have done above)

There is a way to call another language, which is Java method (not javascript) in the script. Just search "Java" at the function. I don't much about how to use it yet. So far I only use java.net.URLDecoder to decode URL back to original (automagic has function encodeURL but there is no decodeURL).

Map Function is used for lookup value, similiar to Vlookup in excel. For example, you have code for product, 00001234; then you want the name of the product. You can maintain a map of lookup, and after this code pass to the map, you have the product name. For your case, it is redundant.

You can just simply create dynamic variable using setValue(). More about dynamic variable at other thread.
http://automagic4android.com/forum/view ... f=5&t=6813

This is you code after remove the map(), leaving only dynamic variable. Of course it only works if you have a fix value for the titlename and umur.

Code: Select all

list=split(file_text, "\\n");
for (i in list)
{
 x=split(i, "=");
 setValue(x[0],x[1]);
}
You can't use x[0]=x[1], that is different with setValue(). Try it out and use debug dialog to check the result.
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.

tetsugami
Posts: 7
Joined: 30 Jul 2017 15:59

Re: [SOLVED] How to get a value from key?

Post by tetsugami » 10 Aug 2017 00:43

Its work :)) my code is simply now,,
You are right, i am use x[0]=x[1] before and its not work,,

Post Reply