Page 1 of 1
A bit more detailed explanation
Posted: 08 Mar 2017 17:24
by husky
Hello,
I'm trying to list the data in a list.
There are, as far as I understand, two ways:
1.
for (i in [x to y]) {
}
2. for (element in list){
}
Questions regarding point 1.
What i stands for?
How do I know what list (assuming many lists in the flow) is being processed?
Could it be written as:
some_variable=length(mylist); // Obtain number of entries in the list
for (i in [1 to some_variable] {
}
Generic:
When should I opt for option 1 or 2?
Thank You
Husky
Re: A bit more detailed explanation
Posted: 09 Mar 2017 21:14
by Martin
Hi,
The expression [x to y] creates a list containing the numbers x, x+1, x+2, ..., y.
You could rewrite your first for loop like this:
Code: Select all
list_with_numbers = [1 to 5];
for (i in list_with_numbers) {
log(i);
}
The for-loop just assigns each value in the list to the specified variable (i) and executes the loop-body with each value. You can use also use any other variable name when it makes sense, for example:
Code: Select all
list_with_numbers = [1 to 5];
for (number in list_with_numbers) {
log(number);
}
You can also loop over the number of elements in the list (accessing list elements is zero-based so I recommend to start at 0):
Code: Select all
mylist = newList("a", "b", "c");
some_variable=length(mylist); // Obtain number of entries in the list
for (i in [0 to some_variable-1]) {
log("Element at index {i} contains: " + mylist[i]);
}
Regards,
Martin
Re: A bit more detailed explanation
Posted: 12 Mar 2017 22:36
by husky
Martin,
Thanks for the reply amd your infinite patience with dumbasses like me.
I'll see how your excellent explanation can be used in my case.
Will get back to you later if you don't mind.
Regards
Husky
Re: A bit more detailed explanation
Posted: 13 Mar 2017 22:43
by husky
Another question:
Is it possible to read a sequential file (flat file) with Automagic?
If so, what function allows me to do it. I couldn't find anything in the Help.
Each record ends with ("\n"}.
Thanks
Husky
Re: A bit more detailed explanation
Posted: 14 Mar 2017 09:21
by Martin
Hi,
Automagic can only read an entire file at once so the file should not be too large and should not be a device that produces an infinite amount of output like /dev/random otherwise Automagic will be stopped with an OutOfMemoryError quite fast.
To read a complete text file and to split the text to a list of lines, you can use the following actions:
-
Init Variable File List: file.txt to file_text
-
Script
Code: Select all
lines = split(file_text, "\\n");
// log each line or otherwise process the content
for (line in lines)
{
log(line);
}
Also check out
Script examples for some other script examples.
Regards,
Martin
Re: A bit more detailed explanation
Posted: 15 Mar 2017 21:03
by husky
Hello Martin,
I guess you will be answering this reply.
I completed my flows based on your hints and they work perfectly (as expected).
However, I have however a question:
I'm trying to use RegExp and could not figure out the code for a particular situation:
When I want to remove (replace) ALL numeric values in a list I can use:
x=replaceAll(list, "\\d", "replacement");
Question:
Given that in a list I have a 10 digit number of unkown value, that I'd like to get rid of, leaving all other eventual numeric digits untouched, what would be the RegExp?
I tried "\\d{10}" , "\d{10}", "[0-9]{10}" , "\\d{10}.*"
The first one, wiped out all numeric digits. Unintended consquences.
The last three won't work.
Thank You
Husky
PS: This example does not work and I'm confused here. The regexp to find "abcd":
x = matches("abcd","^[a-d]{4}$"); works for Java but not for Automagic.
I tried variations based on the Help but none would return TRUE.
Re: A bit more detailed explanation
Posted: 16 Mar 2017 18:38
by Martin
Hi,
The problem most likely lies in the feature of Automagic to replace inline expressions in strings with double quotes. For example this script:
variable1 = "123";
variable2 = "xyz {variable1}";
Will evaluate the value of variable 2 to xyz 123.
This can be a problem in case of RegExs since "\\d{10}" will result in "\\d10" before the RegEx is executed.
You can avoid this problem by using single quotes for the strings:
'\\d{10}'
Automagic will not replace inline expressions in such strings.
Regards,
Martin
Re: A bit more detailed explanation
Posted: 17 Mar 2017 13:27
by husky
Martin,
In all of my research on the topic RegEx, I never came across with the possibility of the solution you gave (change " for ').
As always with me, the problem lies between the chair and the keyboard. Perhaps I should have researched b bit longer and eventually... who knows.
Problem Solved and I can go ahead and work with this new information I got from you.
Thank You very much and I will be out of your hair for as long as I can.
I hate bugging people with idiotic questions.
Best Regards
Husky
Re: A bit more detailed explanation
Posted: 20 Mar 2017 20:53
by Martin
Husky,
No problem and I'm glad it's working
This was certainly not an idiotic question since it's not that common for scripting languages to replace variables within strings. Maybe it would be helpful if Automagic would highlight such variable replacements in strings with the light blue color to show that there might be something special going on --> I'll add it to the list of features under consideration.
Regards,
Martin
Re: A bit more detailed explanation
Posted: 22 Mar 2017 13:38
by husky
Martin,
I'm glad you considered this to be included in the eventual features.
Good call.
Best Regards
Husky