A bit more detailed explanation
Moderator: Martin
A bit more detailed explanation
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
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
"Basic research is what I'm doing when I don't know what I'm doing"
Re: A bit more detailed explanation
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:
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:
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):
Regards,
Martin
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);
}
Code: Select all
list_with_numbers = [1 to 5];
for (number in list_with_numbers) {
log(number);
}
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]);
}
Martin
Re: A bit more detailed explanation
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
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
"Basic research is what I'm doing when I don't know what I'm doing"
Re: A bit more detailed explanation
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
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
"Basic research is what I'm doing when I don't know what I'm doing"
Re: A bit more detailed explanation
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
Also check out Script examples for some other script examples.
Regards,
Martin
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);
}
Regards,
Martin
Re: A bit more detailed explanation
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.
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.
"Basic research is what I'm doing when I don't know what I'm doing"
Re: A bit more detailed explanation
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
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
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
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
"Basic research is what I'm doing when I don't know what I'm doing"
Re: A bit more detailed explanation
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
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
Martin,
I'm glad you considered this to be included in the eventual features.
Good call.
Best Regards
Husky
I'm glad you considered this to be included in the eventual features.
Good call.
Best Regards
Husky
"Basic research is what I'm doing when I don't know what I'm doing"