Board index FlightGear Development Nasal

Strip out the left most numbers in a string  Topic is solved

Nasal is the scripting language of FlightGear.

Strip out the left most numbers in a string

Postby wlbragg » Sun Jan 19, 2020 9:12 am

I'm tired of looking for what should be a simple answer.
I need to split the following using nasal string manipulation.

123 xxxxxxxx xxxx
2 xxxx-xxxx 23xxxx
12 xxxx-23-xxxx 23456

I need a string function, that in all three cases above, will return the first numbers up to the first space.
The format of the string will always be numbers+space+numbers and/or letters+space+repeating.
The original string will always have that one space after the first variable amount of numbers.

The returned strings will then be...
123
2
12
Last edited by wlbragg on Sun Jan 19, 2020 8:17 pm, edited 1 time in total.
Kansas and Ohio/Midwest scenery development.
KEQA, 3AU, KRCP Airport Layout
Intel i7/GeForce RTX 2070/Max-Q
User avatar
wlbragg
 
Posts: 7587
Joined: Sun Aug 26, 2012 12:31 am
Location: Kansas (Tornado Alley), USA
Callsign: WC2020
Version: next
OS: Win10/Linux/RTX 2070

Re: Strip out the left most numbers in a string

Postby legoboyvdlp » Sun Jan 19, 2020 10:33 am

String.split()?

Split at the space and fetch index[0] - everything to the left of the space.
User avatar
legoboyvdlp
 
Posts: 7981
Joined: Sat Jul 26, 2014 2:28 am
Location: Northern Ireland
Callsign: G-LEGO
Version: next
OS: Windows 10 HP

Re: Strip out the left most numbers in a string

Postby Hooray » Sun Jan 19, 2020 11:52 am

that's the right answer, for the details check out: http://wiki.flightgear.org/Nasal_String_Manipulation

The low-level method without using the string object, is using native Nasal library calls:

http://wiki.flightgear.org/Nasal_library#split.28.29

There are also the corresponding find(), left(), right(), num() and substr() APIs to implement arbitrary heuristics: http://wiki.flightgear.org/Nasal_librar ... _functions
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: Strip out the left most numbers in a string

Postby wlbragg » Sun Jan 19, 2020 8:41 pm

You realize your both absolutely no help, right! :lol:

I have been spiting and parsing string going on 30 years, I know most of the tools available.
I don't know how to apply them in this case. :oops:

I need details, and not just split at the space or find the index of the space and trim to the right.

I need the examples, docs don't have the needed examples, it only has the tool.

Actually, what I really want it the exact sting parsing algorithm. :D 8) :roll:

The original_string string actually looks like these examples.

xxxxx1 xxxxxxxx xxxx
xxxxx2 xxxx-xxxx 23xxxx
....
xxxxx12 xxxx-23-xxxx 23456
...
xxxxx121 xxxxxxxx

What I already have is...
Code: Select all
var parsed_string = string.trim(original_string, -1, string.isalpha);


Gives me the problem I propose
1 xxxxxxxx xxxx
2 xxxx-xxxx 23xxxx
....
12 xxxx-23-xxxx 23456
...
121 xxxxxxxx

var parsed_string = string.trim(original_string, 1, string.isalpha);
I guess doesn't work because it hits the space?

Neither of these work either
var parsed_string = string.trim(original_string, -1, string.isspace);
var parsed_string = string.trim(original_string, -1, string.isblank);

I tried looping through the string to get the index of the first space, using for loop and forindex loop.
But I think syntax is wrong using for loop or forindex can't be used this way.
Code: Select all
var ndx = -1;

for (i=0; i<utf8.size(parsed_string); i++) {
    if (utf8.strc(parsed_string, nx) == ' ') {
        ndx=nx;
        break;
    }
}

forindex (var nx; utf8.size(parsed_string)) {
    if (utf8.strc(parsed_string, nx) == ' ') {
        ndx=nx;
        break;
    }
}


Then next would be.
Code: Select all
var final_string = utf8.substr(parsed_string, 0, ndx);


There should be a way to find the first space using substring alone, but I can't find it.

IE:
Code: Select all
var final_string = utf8.substr(parsed_string, 0, index_of_first_space);
Kansas and Ohio/Midwest scenery development.
KEQA, 3AU, KRCP Airport Layout
Intel i7/GeForce RTX 2070/Max-Q
User avatar
wlbragg
 
Posts: 7587
Joined: Sun Aug 26, 2012 12:31 am
Location: Kansas (Tornado Alley), USA
Callsign: WC2020
Version: next
OS: Win10/Linux/RTX 2070

Re: Strip out the left most numbers in a string

Postby wlbragg » Sun Jan 19, 2020 9:04 pm

All I need is the equivalent of indexOf such as

Code: Select all
var final_string = string.trim(original_string, 1, original_string.indexOf(" "));

or
Code: Select all
var final_string = utf8.substr(original_string, 0, original_string.indexOf(" "))-1

Does nasal have such a tool?
Kansas and Ohio/Midwest scenery development.
KEQA, 3AU, KRCP Airport Layout
Intel i7/GeForce RTX 2070/Max-Q
User avatar
wlbragg
 
Posts: 7587
Joined: Sun Aug 26, 2012 12:31 am
Location: Kansas (Tornado Alley), USA
Callsign: WC2020
Version: next
OS: Win10/Linux/RTX 2070

Re: Strip out the left most numbers in a string

Postby wlbragg » Sun Jan 19, 2020 9:08 pm

Even an example of using split() would probably get me what I need if it can split at spaces, I have never used it and don't know how it works exactly.
Kansas and Ohio/Midwest scenery development.
KEQA, 3AU, KRCP Airport Layout
Intel i7/GeForce RTX 2070/Max-Q
User avatar
wlbragg
 
Posts: 7587
Joined: Sun Aug 26, 2012 12:31 am
Location: Kansas (Tornado Alley), USA
Callsign: WC2020
Version: next
OS: Win10/Linux/RTX 2070

Re: Strip out the left most numbers in a string

Postby legoboyvdlp » Sun Jan 19, 2020 9:10 pm

Code: Select all

var string = "text 1234";
var string1 = split(" ", string);
print(string1[0]);



The output should be "text".
User avatar
legoboyvdlp
 
Posts: 7981
Joined: Sat Jul 26, 2014 2:28 am
Location: Northern Ireland
Callsign: G-LEGO
Version: next
OS: Windows 10 HP

Re: Strip out the left most numbers in a string

Postby wlbragg » Sun Jan 19, 2020 9:38 pm

I wish!

Nasal runtime error: non-scalar in string context
Kansas and Ohio/Midwest scenery development.
KEQA, 3AU, KRCP Airport Layout
Intel i7/GeForce RTX 2070/Max-Q
User avatar
wlbragg
 
Posts: 7587
Joined: Sun Aug 26, 2012 12:31 am
Location: Kansas (Tornado Alley), USA
Callsign: WC2020
Version: next
OS: Win10/Linux/RTX 2070

Re: Strip out the left most numbers in a string

Postby Hooray » Sun Jan 19, 2020 10:34 pm

wlbragg wrote in Sun Jan 19, 2020 9:08 pm:Even an example of using split() would probably get me what I need if it can split at spaces, I have never used it and don't know how it works exactly.


That's exactly why I posted those links, the low-level split() function is documented here: http://wiki.flightgear.org/Nasal_library#split.28.29

The short answer is: split will return a vector (think array or list) of strings by splitting the string argument using the token/delimiter you pass to it.
You can obtain the size of the vector (list) by using size(vector), the first result can be addressed via [0], the 2nd via [1]

In other words, you need to understand vector handling to make heads and tails of code like this:

Code: Select all

var text = "foo bar";
var delimiter = " ";
var result_list = split(delimiter, text);
debug.dump(result_list);



Under the hood, this will call the equivalent of the find() API detailed above, i.e. look for the "needle" in the "haystack" and return a vector of results.

It makes sense to use the Nasal console when testing this, and test things line by line to see where things aren't working properly, and then take it from there.

Is the example isn't working (correctly), the wiki/code or examples may need to be updated
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: Strip out the left most numbers in a string

Postby wlbragg » Sun Jan 19, 2020 10:52 pm

Right, I looked at that example. It didn't explain the details of what I needed. 1, I didn't know what a space is represented as. Is it a quotes with the space, is it an ASCII space character, it doesn't state those kind of details. Using what lego gave me, appears to result in some kind of a nasal error.
I know the tools are there, can't figure out the right tool, the right syntax.
Kansas and Ohio/Midwest scenery development.
KEQA, 3AU, KRCP Airport Layout
Intel i7/GeForce RTX 2070/Max-Q
User avatar
wlbragg
 
Posts: 7587
Joined: Sun Aug 26, 2012 12:31 am
Location: Kansas (Tornado Alley), USA
Callsign: WC2020
Version: next
OS: Win10/Linux/RTX 2070

Re: Strip out the left most numbers in a string

Postby Hooray » Sun Jan 19, 2020 11:26 pm

May I suggest that you simply fire up the Nasal Console and start tinkering with different variations of the code we've posted here, or the code shown in the wiki ?
It will really only begin to make sense once you have tried simple versions of these examples, and if you don't understand how vectors work, use debug.dump() instead of print()

The questions you are posing sound like you've got a background in background dating back to the 80s, i.e. it sounds pretty low-level for something as simple as Nasal.
Likewise, the for-loops you have posted would suggest that you are hoping to implement an algorithm, whereas all you need is call an existing function.

For the time being, I would only tinker with those examples, and ignore your actual use-case, and only once the examples are working as expected, you can tweak those to adapt them for your specific use-case/application.

If your background is indeed in 80s style coding, I would suggest to ignore the string class, and only use the low-level split() function for now, and then take it from there.

Once the examples are working as expected, it may make sense to read up on Nasal vectors (wiki) if the concept of a resizable array is new to you.

Again, playing 30 minutes with the Nasal console and different variations of a really simple example (by changing the delimiter and the string to be searched), will make things much more plausible than reading stuff that contains a ton of new words and concepts
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: Strip out the left most numbers in a string

Postby wlbragg » Sun Jan 19, 2020 11:32 pm

Thanks for the suggestion Hooray. I really should refresh my basics, but I don't have the time to "tinker". I had a specific "simple" problem that most core devs will know the answer to and I wanted it last night.

Thanks lego, that error was my fault. I didn't catch after the split that string1 was an array needing string1[0]. It works fine, thanks again.
Kansas and Ohio/Midwest scenery development.
KEQA, 3AU, KRCP Airport Layout
Intel i7/GeForce RTX 2070/Max-Q
User avatar
wlbragg
 
Posts: 7587
Joined: Sun Aug 26, 2012 12:31 am
Location: Kansas (Tornado Alley), USA
Callsign: WC2020
Version: next
OS: Win10/Linux/RTX 2070

Re: Strip out the left most numbers in a string  

Postby wlbragg » Sun Jan 19, 2020 11:38 pm

My defense for not catching that string1 was an array is because I'm trying to watch a football game at the same time. Much appreciated though, thank you.

Final answer
Code: Select all
var stringa = split(" ", string.trim(org_string, -1, string.isalpha));
var new_string = stringa[0];
Kansas and Ohio/Midwest scenery development.
KEQA, 3AU, KRCP Airport Layout
Intel i7/GeForce RTX 2070/Max-Q
User avatar
wlbragg
 
Posts: 7587
Joined: Sun Aug 26, 2012 12:31 am
Location: Kansas (Tornado Alley), USA
Callsign: WC2020
Version: next
OS: Win10/Linux/RTX 2070


Return to Nasal

Who is online

Users browsing this forum: No registered users and 3 guests