Board index FlightGear Development Nasal

Manipulating Strings with Nasal

Nasal is the scripting language of FlightGear.

Manipulating Strings with Nasal

Postby it0uchpods » Sat May 13, 2017 3:56 pm

Hi

EDIT: See posts below

I need to combine 2 strings. How can I do this in a setprop?

Here is what I want it to do, this won't work however, since + expects a number.

Code: Select all
var button = func(btn) {
   var scratchpad = getprop("/MCDU[0]/scratchpad");
   if (btn == "A") {
      setprop("/MCDU[0]/scratchpad", scratchpad + "A");
   } else if (btn == "B") {
      setprop("/MCDU[0]/scratchpad", scratchpad + "B");
   }
}


Thanks,
Josh
Last edited by it0uchpods on Mon May 15, 2017 10:39 pm, edited 1 time in total.
it0uchpods/WTF411

Do not expect immediate responses. I do not log in often. I do not answer PMs from non-admins.
I only monitor IDG Development and Support Threads. Everything else is ignored.
Download High Quality Aircraft from it0uchpods Hanger
User avatar
it0uchpods
 
Posts: 3348
Joined: Tue Oct 06, 2015 12:51 pm
Version: 2018.2.1
OS: Windows 10 x64

Re: Combine string in setprop?

Postby Necolatis » Sat May 13, 2017 4:14 pm

use tilde: ~
"Airplane travel is nature's way of making you look like your passport photo."
— Al Gore


Hangar: https://sites.google.com/site/fghangar/
User avatar
Necolatis
 
Posts: 1833
Joined: Mon Oct 29, 2012 12:40 am
Location: EKOD
Callsign: Leto
IRC name: Neco
Version: 2018.1.1
OS: Windows 10 Pro

Re: Combine string in setprop?

Postby it0uchpods » Sat May 13, 2017 4:40 pm

Thanks,
Can you give me an example?

Josh
it0uchpods/WTF411

Do not expect immediate responses. I do not log in often. I do not answer PMs from non-admins.
I only monitor IDG Development and Support Threads. Everything else is ignored.
Download High Quality Aircraft from it0uchpods Hanger
User avatar
it0uchpods
 
Posts: 3348
Joined: Tue Oct 06, 2015 12:51 pm
Version: 2018.2.1
OS: Windows 10 x64

Re: Combine string in setprop?

Postby Necolatis » Sat May 13, 2017 4:51 pm

Code: Select all
var button = func(btn) {
   var scratchpad = getprop("/MCDU[0]/scratchpad");
   if (btn == "A") {
      setprop("/MCDU[0]/scratchpad", scratchpad ~ "A");
   } elsif (btn == "B") {
      setprop("/MCDU[0]/scratchpad", scratchpad ~ "B");
   }
}
"Airplane travel is nature's way of making you look like your passport photo."
— Al Gore


Hangar: https://sites.google.com/site/fghangar/
User avatar
Necolatis
 
Posts: 1833
Joined: Mon Oct 29, 2012 12:40 am
Location: EKOD
Callsign: Leto
IRC name: Neco
Version: 2018.1.1
OS: Windows 10 Pro

Re: Combine string in setprop?

Postby it0uchpods » Sat May 13, 2017 4:55 pm

Thank you!

Kind Regards,
Josh
it0uchpods/WTF411

Do not expect immediate responses. I do not log in often. I do not answer PMs from non-admins.
I only monitor IDG Development and Support Threads. Everything else is ignored.
Download High Quality Aircraft from it0uchpods Hanger
User avatar
it0uchpods
 
Posts: 3348
Joined: Tue Oct 06, 2015 12:51 pm
Version: 2018.2.1
OS: Windows 10 x64

Re: Combine string in setprop?

Postby it0uchpods » Sat May 13, 2017 5:04 pm

Is something like this possible to remove the last letter? I need it for the CLR button on the MCDU

http://stackoverflow.com/questions/2310 ... m-c-string

Sorry if it's obvious.
it0uchpods/WTF411

Do not expect immediate responses. I do not log in often. I do not answer PMs from non-admins.
I only monitor IDG Development and Support Threads. Everything else is ignored.
Download High Quality Aircraft from it0uchpods Hanger
User avatar
it0uchpods
 
Posts: 3348
Joined: Tue Oct 06, 2015 12:51 pm
Version: 2018.2.1
OS: Windows 10 x64

Re: Combine string in setprop?

Postby Necolatis » Sat May 13, 2017 5:13 pm

Yes, check out substr() on wiki, it can do stuff like that.
"Airplane travel is nature's way of making you look like your passport photo."
— Al Gore


Hangar: https://sites.google.com/site/fghangar/
User avatar
Necolatis
 
Posts: 1833
Joined: Mon Oct 29, 2012 12:40 am
Location: EKOD
Callsign: Leto
IRC name: Neco
Version: 2018.1.1
OS: Windows 10 Pro

Re: Combine string in setprop?

Postby it0uchpods » Sat May 13, 2017 5:19 pm

OK will do.

Thanks again,
Josh
it0uchpods/WTF411

Do not expect immediate responses. I do not log in often. I do not answer PMs from non-admins.
I only monitor IDG Development and Support Threads. Everything else is ignored.
Download High Quality Aircraft from it0uchpods Hanger
User avatar
it0uchpods
 
Posts: 3348
Joined: Tue Oct 06, 2015 12:51 pm
Version: 2018.2.1
OS: Windows 10 x64

Re: Combine string in setprop?

Postby it0uchpods » Sun May 14, 2017 5:05 pm

I've taken a look, and I understand how it works (I think)
But the string amount can vary, and I want to remove the last letter regardless of the string.
I tried this:
Code: Select all
setprop("/MCDU[0]/scratchpad", substr(scratchpad, -1));


What can I do?
it0uchpods/WTF411

Do not expect immediate responses. I do not log in often. I do not answer PMs from non-admins.
I only monitor IDG Development and Support Threads. Everything else is ignored.
Download High Quality Aircraft from it0uchpods Hanger
User avatar
it0uchpods
 
Posts: 3348
Joined: Tue Oct 06, 2015 12:51 pm
Version: 2018.2.1
OS: Windows 10 x64

Re: Combine string in setprop?

Postby jam007 » Sun May 14, 2017 6:05 pm

A possible solution:
Code: Select all
var shorterby1 = "";
if (size(mystring) > 1) {
  shorterby1=left(mystring, size(mystring)-1);
}

See Nasal library
jam007
 
Posts: 477
Joined: Sun Dec 16, 2012 10:04 am
Location: Uppsala, Sweden
Version: 2017.3.1
OS: Ubuntu 16.04

Re: Combine string in setprop?

Postby it0uchpods » Mon May 15, 2017 6:36 pm

@jam007
That was it!
Perfect!

Here is my code:

Code: Select all
   } else if (btn == "CLR") {
      var scratchpad = getprop("/MCDU[0]/scratchpad");
      if (size(scratchpad) > 0) {
         setprop("/MCDU[0]/scratchpad", left(scratchpad, size(scratchpad)-1));
      }
   }
it0uchpods/WTF411

Do not expect immediate responses. I do not log in often. I do not answer PMs from non-admins.
I only monitor IDG Development and Support Threads. Everything else is ignored.
Download High Quality Aircraft from it0uchpods Hanger
User avatar
it0uchpods
 
Posts: 3348
Joined: Tue Oct 06, 2015 12:51 pm
Version: 2018.2.1
OS: Windows 10 x64

Re: Manipulating Strings with Nasal

Postby it0uchpods » Mon May 15, 2017 10:40 pm

Hi
Another question.

Lets say I get from the user KSFO/KNUQ

How would I take that apart, and put it into KSFO and KNUQ ?

I haven't seen anything on the wiki which talks about this.

Kind Regards,
Josh
it0uchpods/WTF411

Do not expect immediate responses. I do not log in often. I do not answer PMs from non-admins.
I only monitor IDG Development and Support Threads. Everything else is ignored.
Download High Quality Aircraft from it0uchpods Hanger
User avatar
it0uchpods
 
Posts: 3348
Joined: Tue Oct 06, 2015 12:51 pm
Version: 2018.2.1
OS: Windows 10 x64

Re: Manipulating Strings with Nasal

Postby sanhozay » Mon May 15, 2017 10:54 pm

sanhozay
 
Posts: 1207
Joined: Thu Dec 26, 2013 11:57 am
Location: EGNM
Callsign: G-SHOZ
Version: Git
OS: Ubuntu 16.04

Re: Manipulating Strings with Nasal

Postby it0uchpods » Tue May 16, 2017 1:40 am

Thank you for pointing me to this resource. You guys are the best :)

Kind Regards,
Josh
it0uchpods/WTF411

Do not expect immediate responses. I do not log in often. I do not answer PMs from non-admins.
I only monitor IDG Development and Support Threads. Everything else is ignored.
Download High Quality Aircraft from it0uchpods Hanger
User avatar
it0uchpods
 
Posts: 3348
Joined: Tue Oct 06, 2015 12:51 pm
Version: 2018.2.1
OS: Windows 10 x64

Re: Manipulating Strings with Nasal

Postby it0uchpods » Thu May 18, 2017 8:02 pm

OK another question.
How I can figure if it is an int or not?

var crz = int(scratchpad);

But is they a way I can do a true false to find if its an int or not? As if my code currently:

Code: Select all
         var crz = int(scratchpad);
         var crzs = size(scratchpad);
         if (crzs >= 1 and crzs <= 3) {
            if (crz > 0 and crz <= 430) {


But here is problem. If a person enters a rubbish value like /55, then you get a nasal error nil-used in generic context.

Kind Regards,
Josh
it0uchpods/WTF411

Do not expect immediate responses. I do not log in often. I do not answer PMs from non-admins.
I only monitor IDG Development and Support Threads. Everything else is ignored.
Download High Quality Aircraft from it0uchpods Hanger
User avatar
it0uchpods
 
Posts: 3348
Joined: Tue Oct 06, 2015 12:51 pm
Version: 2018.2.1
OS: Windows 10 x64

Next

Return to Nasal

Who is online

Users browsing this forum: No registered users and 2 guests