Simple programming question

Forum to discuss everything related to the current development build of Automagic.

Moderator: Martin

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

Simple programming question

Post by husky » 27 Dec 2016 18:30

Hello,

I have a programming question that is best explained by giving a working example.

OBJECTIVE: Have the flows and the duration of each one as given by getFlowStatisticsDuration().

The Flow:
list_keys= newList();
list_values= newList();
duration= getFlowStatisticsDuration();
size = length(getFlowStatisticsDuration()); //size may vary from one run from another run.
list_keys=getMapKeys(getFlowStatisticsDuration());
list_values=getMapValues(getFlowStatisticsDuration());
a=0;
while (a < size){
list_keys[a];
list_values[a];
a=a+1; // no a++ accepted. Weird.
{

Once this is set up, all I need is to write the file with the pairs built in the "while" loop.
This is the trivial part.
Please pardon my ignorance from this point on. :oops:


MY SOLUTION:Probably not that smart I'd say: :oops:

in the text in the Write to File i blindly define the entries as follows:

{list_key[0]} {list_values[0]}
...
{list_key[n]} {list_values[n]} Note: "n" may be smaller or greater than variable size above

ANALYSIS:

The above method of defining the output to be "printed" may not bring all entries or will have too many of them predefined in this case, a lot of {error} will be printed since predefined entries are more them the real data collected.


QUESTION:
Is there a better way to write the output in such a way that it could be controlled by the variable size?
In other words, no need to predefine the number of

{list_key[0]} {list_values[0]}
...
{list_key[n]} {list_values[n]}

and make it happen programmatically?


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: Simple programming question

Post by Martin » 28 Dec 2016 15:03

Hi,

I would prepare the entire text in the script.

Following script should work:

Code: Select all

stats=getFlowStatisticsDuration();
keys=getMapKeys(stats);

output="";
for (key in keys)
{
  output = output + key + ": " + stats[key] + "\n";
}
and then just use action Write to File: {output}.

Regards,
Martin

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

Re: Simple programming question

Post by husky » 28 Dec 2016 18:44

Martin,

Thanks for thr reply.

I did not know that this could be done inside the Script. I though once you have everything squared out in the Script, the only way to proceed to the output would be adapting the Write to File in the idiot way I was doing it.

Your example is PERFECT. Will save me a lot of time.


Thank You and have a Happy New Year


Husky
"Basic research is what I'm doing when I don't know what I'm doing"

Locked