Board index FlightGear Development Nasal

How to remove a specific item from a Nasal vector ?  Topic is solved

Nasal is the scripting language of FlightGear.

How to remove a specific item from a Nasal vector ?

Postby TheEagle » Wed Jan 05, 2022 5:20 am

I'm reading through the Nasal docs since half an hour … I'm looping over a Nasal vector with a forindex loop, and I want to delete some items by their index. How do I do that ? I've tried delete(<vector>, <index>) but that threw an error bad/missing argument to delete().
Cessna 210 (Wiki)
My other aircraft: my wiki profile !
Other: FGTools (GitHub)
World tour: View on SkyVector
Please consider donating $1 / €1 to help me finance a new camera !
User avatar
TheEagle
 
Posts: 3411
Joined: Sat May 01, 2021 3:27 pm
Location: France
Pronouns: You, he
Callsign: F-EAGLE
IRC name: none
Version: Git next
OS: Ubuntu Studio 22.04

Re: How to remove a specific item from a Nasal vector ?

Postby Thorsten » Wed Jan 05, 2022 9:04 am

Write your own custom function, it's a two liner (if I remember correctly, I used splitting/joining the array for the purpose).

The Shuttle code has this as a remove_from_vector() function somewhere, if you're lazy you can search it.
Thorsten
 
Posts: 12490
Joined: Mon Nov 02, 2009 9:33 am

Re: How to remove a specific item from a Nasal vector ?

Postby Hooray » Wed Jan 05, 2022 12:15 pm

delete() is for hashes only: https://wiki.flightgear.org/Nasal_library#delete.28.29

To remove the last element of a vector, use the pop() API: https://wiki.flightgear.org/Nasal_library#pop.28.29

To create a new, sub vector, you can simply use vector slicing using the dedicated subvec() API: https://wiki.flightgear.org/Nasal_library#subvec.28.29
There's also short-hand (syntax-only) support: https://wiki.flightgear.org/Nasal_Varia ... or_slicing

Alternatively, use a higher level wrapper: https://wiki.flightgear.org/Vector

Sorry that it took you so long to figure this out, if in doubt, please feel free to get involved in improving the Nasal docs / wiki
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 12707
Joined: Tue Mar 25, 2008 9:40 am
Pronouns: THOU

Re: How to remove a specific item from a Nasal vector ?

Postby ludomotico » Wed Jan 05, 2022 12:29 pm

In case you are not aware, how to remove items from a list while you are inside a loop of the same list is a classic problem in coding

One option is you create a new list inside the loop, appending elements or not as you see fit, and, once the loop is finished, you swap the old and the new lists.
User avatar
ludomotico
 
Posts: 1269
Joined: Tue Apr 24, 2012 2:01 pm
Version: nightly
OS: Windows 10

Re: How to remove a specific item from a Nasal vector ?

Postby TheEagle » Wed Jan 05, 2022 1:23 pm

Hooray wrote in Wed Jan 05, 2022 12:15 pm:To remove the last element of a vector, use the pop() API: https://wiki.flightgear.org/Nasal_library#pop.28.29

Well I don't need to remove the last item, but rather the item at a specific index !

Hooray wrote in Wed Jan 05, 2022 12:15 pm:Alternatively, use a higher level wrapper: https://wiki.flightgear.org/Vector

So instead of vectorVar = []; I would use vectorVar = std.Vector.new() ?

Hooray wrote in Wed Jan 05, 2022 12:15 pm:Sorry that it took you so long to figure this out, if in doubt, please feel free to get involved in improving the Nasal docs / wiki

Hmm, well, I think it should be emphasized that there is no way to just delete a variable (as I am used to from Python :mrgreen: )
Cessna 210 (Wiki)
My other aircraft: my wiki profile !
Other: FGTools (GitHub)
World tour: View on SkyVector
Please consider donating $1 / €1 to help me finance a new camera !
User avatar
TheEagle
 
Posts: 3411
Joined: Sat May 01, 2021 3:27 pm
Location: France
Pronouns: You, he
Callsign: F-EAGLE
IRC name: none
Version: Git next
OS: Ubuntu Studio 22.04

Re: How to remove a specific item from a Nasal vector ?  

Postby Hooray » Wed Jan 05, 2022 1:32 pm

vector slicing should do exactly what you want - if you know JavaScript or Python, you are probably already familiar with the concept:

https://wiki.flightgear.org/Nasal_Varia ... or_slicing
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 12707
Joined: Tue Mar 25, 2008 9:40 am
Pronouns: THOU

Re: How to remove a specific item from a Nasal vector ?

Postby TheEagle » Wed Jan 05, 2022 1:50 pm

Yes, if that's the way to do it, it's no problem ! :) thank you for your help !
Cessna 210 (Wiki)
My other aircraft: my wiki profile !
Other: FGTools (GitHub)
World tour: View on SkyVector
Please consider donating $1 / €1 to help me finance a new camera !
User avatar
TheEagle
 
Posts: 3411
Joined: Sat May 01, 2021 3:27 pm
Location: France
Pronouns: You, he
Callsign: F-EAGLE
IRC name: none
Version: Git next
OS: Ubuntu Studio 22.04

Re: How to remove a specific item from a Nasal vector ?

Postby Hooray » Wed Jan 05, 2022 4:13 pm

If you're looking for a dedicated API/function, just use the aforementioned subvec() API (see the wiki for details)
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 12707
Joined: Tue Mar 25, 2008 9:40 am
Pronouns: THOU


Return to Nasal

Who is online

Users browsing this forum: No registered users and 2 guests