A little help on dealing with Map

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 little help on dealing with Map

Post by husky » 03 Apr 2017 15:58

Hello,

"Basic Research is what I do when I don't know what I'm doing".

So in my Basic Research i'm using the getFlowStatisticsDuration(). As per Help, it looks to me that the return from the function is a Map.

How do I get the key/value pair from the function.
I know how to add pairs manually to a Map but can't figure out how to get hold of the existing ones in the function.
I tried many variations before posting but all ended up nowhere.


Thank You

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

User avatar
Scotty
Posts: 78
Joined: 26 Aug 2016 20:29
Location: Southern California

Re: A little help on dealing with Map

Post by Scotty » 03 Apr 2017 21:36

Let's say that your current script is:

HuskyFlowStats=getFlowStatisticsDuration();

The variable HuskyFlowStats is a comma-separated map of every key/value pair (example: {FlowName1=45665, FlowName2=7534, FlowName3=....} )

The next lines in your script are determined by exactly what you want to extract. Say you want to extract the name of the longest-duration flow, and its actual duration. You could get that by adding the following lines to the script:

start=indexOf(HuskyFlowStats, "{", + 1);// sets the start point at the position after the squiggly bracket
end=indexOf(HuskyFlowStats, ",", start);// sets the end point at the first comma after "start"
LongestFlow = substring(HuskyFlowStats, start, end);//extracts, as a string, the characters delimited above

Assuming we're starting with the values (in HuskyFlowStats) that I provided above, then the variable LongestFlow will contain the key/value pair FlowName1=45665

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

Re: A little help on dealing with Map

Post by Martin » 04 Apr 2017 15:21

Hi,

You can get the keys and values of a map with functions getMapKeys and getMapValues.
Please also check out the script example page:
Script examples (scroll down to the Maps section).

You could use following script to get the flow with the longest duration:
stats = getFlowStatisticsDuration();
key = getMapKeys(stats)[0];//the first key
value = stats[key];//get the value for the key

Regards,
Martin

User avatar
Scotty
Posts: 78
Joined: 26 Aug 2016 20:29
Location: Southern California

Re: A little help on dealing with Map

Post by Scotty » 04 Apr 2017 15:30

Thanks Martin - I knew how to get all keys (or all values) using the getMapKeys (or getMapValues), and I knew that you could find the value of a key whose name you already know, using "KeyName"; but I didn't know that you could get the name / value of a key (without knowing its name) by its zero-based index position in the map. Very useful, and a good deal simpler than what I posted.

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

Re: A little help on dealing with Map

Post by husky » 04 Apr 2017 18:50

To Martin and Scotty and all of you who had the patience to bear with me.

Thanks both for the information given in your reply. Both are useful for what I have in my plans for the Flow I'm developing. So far only 86 of them and learning. :D

The problem is that I'm running out of new ideas. WIll keep trying though.


Best Regards

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

Post Reply