A bit more detailed explanation

Post your questions and help other users.

Moderator: Martin

Post Reply
User avatar
husky
Posts: 132
Joined: 29 Oct 2016 13:41
Location: Omaha, Nebraska, USA
Contact:

A bit more detailed explanation

Post by husky » 08 Mar 2017 17:24

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
"Basic research is what I'm doing when I don't know what I'm doing"

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

Re: A bit more detailed explanation

Post by Martin » 09 Mar 2017 21:14

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

User avatar
husky
Posts: 132
Joined: 29 Oct 2016 13:41
Location: Omaha, Nebraska, USA
Contact:

Re: A bit more detailed explanation

Post by husky » 12 Mar 2017 22:36

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
"Basic research is what I'm doing when I don't know what I'm doing"

User avatar
husky
Posts: 132
Joined: 29 Oct 2016 13:41
Location: Omaha, Nebraska, USA
Contact:

Re: A bit more detailed explanation

Post by husky » 13 Mar 2017 22:43

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
"Basic research is what I'm doing when I don't know what I'm doing"

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

Re: A bit more detailed explanation

Post by Martin » 14 Mar 2017 09:21

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

User avatar
husky
Posts: 132
Joined: 29 Oct 2016 13:41
Location: Omaha, Nebraska, USA
Contact:

Re: A bit more detailed explanation

Post by husky » 15 Mar 2017 21:03

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.
"Basic research is what I'm doing when I don't know what I'm doing"

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

Re: A bit more detailed explanation

Post by Martin » 16 Mar 2017 18:38

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

User avatar
husky
Posts: 132
Joined: 29 Oct 2016 13:41
Location: Omaha, Nebraska, USA
Contact:

Re: A bit more detailed explanation

Post by husky » 17 Mar 2017 13:27

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
"Basic research is what I'm doing when I don't know what I'm doing"

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

Re: A bit more detailed explanation

Post by Martin » 20 Mar 2017 20:53

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

User avatar
husky
Posts: 132
Joined: 29 Oct 2016 13:41
Location: Omaha, Nebraska, USA
Contact:

Re: A bit more detailed explanation

Post by husky » 22 Mar 2017 13:38

Martin,

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"

Post Reply