Skip to main content

Initializing an Empty List

  • June 3, 2024
  • 4 replies
  • 113 views
  • Translate

Hello,

With the actual version of NQE not providing direct way to initialise an Empty list 

ex:

let empty_list = []

or

empty_list = []

 

A workaround for this is to construct the creation of that list and providing a value with the same data type you need

// an empty List<String>
FALLBACK =
foreach x in [""]
where x != ""
select x;

 

// an empty list of Numbers
FALLBACK_NUMBERS=
foreach x in [0]
where x != 0
select x;

 

Taking this logic further we can create a function that provides us an empty list of whatever generic type. by taking a default value of that type as an input, then comparing against that value

 

The below block contains the full PoC code

// an empty List<String>
FALLBACK =
foreach x in [""]
where x != ""
select x;

// an empty list of Numbers
FALLBACK_NUMBERS=
foreach x in [0]
where x != 0
select x;

// An empty list of a generic type T
emptyListT(default_val)=
foreach x in [default_val]
where x != default_val
select x;

foreach a in [""]
let empty_list_of_strings = FALLBACK
let empty_list_of_numbers = FALLBACK_NUMBERS

// this provides a List<IpAddress>
let empty_list_of_ips = emptyListT(ipAddress("127.0.0.1"))

foreach ip in empty_list_of_ips
select {ip}

Finally, Thanks to @cariddir for encouraging me to start publishing :)

Forum|alt.badge.img+4
  • Spotter
  • June 4, 2024

We can complement this with some extra functions, to add/remove items from a list


addToList(value, list) =
  min(foreach x in [0]
      select if value in list then list else list + [value]);

removeFromList(value, list) =
  min(foreach x in [0]
      select list - [value]);

sample use of this as shown below

foreach x in [0]
// this provides a List<IpAddress>
let iplist = emptyListT(ipAddress("127.0.0.1"))
let iplist = addToList(ipAddress("1.1.1.1"), iplist)
// this next line won't be added (duplicate)
let iplist = addToList(ipAddress("1.1.1.1"), iplist)
// 1.1.1.2 will be added
let iplist = addToList(ipAddress("1.1.1.2"), iplist)
// 1.1.1.1 will be removed
let iplist = removeFromList(ipAddress("1.1.1.1"), iplist)
// 1.1.1.4 won't be removed, although it's not included in the list in any case.
let iplist = removeFromList(ipAddress("1.1.1.4"), iplist)
foreach ip in iplist
select { ip }

the expected output is 1.1.1.2

Translate

Tyson Henrie
Employee
Forum|alt.badge.img+2

Ahmed and Andy,

 

Thank you for providing this information.  However,  NQE is not like Python.  You don’t need to initialize a list before you added entries to it.  You can create a list an any time. 

I have had to use an Empty list for times when I adding things like two records together, that don’t all have the same fields.  For example, the query from the Forward Library FQ_d885c4d4d82ea9d4fee7469bed2b66e1df77509e.  In this case it has an “emptyNumberList = fromTo(1, 0);” because you have interfaces that are not trunks that would be added to lists of interface that are trunks.  Then the interface that is not a trunk would have an empty vlan list.

You can also add things into a list with a plus sign.  You can add lists together.

 

Initializing an empty list may be something you are used to, so you can probably still work that way.  However, I just wanted to point out that it is not required in the NQE language.  Notice, I don’t need to initialize “twoLists” or “threeLists”.

I apologize if I’m interpreting your post wrong.

Regards,

-Tyson

Translate

Tyson Henrie
Employee
Forum|alt.badge.img+2

Oh,  and I forgot to show subtracting a entry from a list.

 

 

Regards,

-Tyson

Translate

Forum|alt.badge.img+4
  • Spotter
  • June 5, 2024

Thanks @Tyson Henrie for the clarification. 

 

Translate

Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings