Board index FlightGear Development Nasal

Manipulating Strings with Nasal

Nasal is the scripting language of FlightGear.

Manipulating Strings with Nasal

Postby Octal450 » Sat May 13, 2017 4: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 Octal450 on Mon May 15, 2017 11:39 pm, edited 1 time in total.
Skillset: JSBsim Flight Dynamics, Systems, Canvas, Autoflight/Control, Instrumentation, Animations
Aircraft: A320-family, MD-11, MD-80, Contribs in a few others

Octal450's GitHub|Launcher Catalog
|Airbus Dev Discord|Octal450 Hangar Dev Discord
User avatar
Octal450
 
Posts: 5583
Joined: Tue Oct 06, 2015 1:51 pm
Location: Huntsville, AL
Callsign: WTF411
Version: next
OS: Windows 11

Re: Combine string in setprop?

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

use tilde: ~
"Airplane travel is nature's way of making you look like your passport photo."
— Al Gore
User avatar
Necolatis
 
Posts: 2233
Joined: Mon Oct 29, 2012 1:40 am
Location: EKOD
Callsign: Leto
IRC name: Neco
Version: 2020.3.19
OS: Windows 10

Re: Combine string in setprop?

Postby Octal450 » Sat May 13, 2017 5:40 pm

Thanks,
Can you give me an example?

Josh
Skillset: JSBsim Flight Dynamics, Systems, Canvas, Autoflight/Control, Instrumentation, Animations
Aircraft: A320-family, MD-11, MD-80, Contribs in a few others

Octal450's GitHub|Launcher Catalog
|Airbus Dev Discord|Octal450 Hangar Dev Discord
User avatar
Octal450
 
Posts: 5583
Joined: Tue Oct 06, 2015 1:51 pm
Location: Huntsville, AL
Callsign: WTF411
Version: next
OS: Windows 11

Re: Combine string in setprop?

Postby Necolatis » Sat May 13, 2017 5: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
User avatar
Necolatis
 
Posts: 2233
Joined: Mon Oct 29, 2012 1:40 am
Location: EKOD
Callsign: Leto
IRC name: Neco
Version: 2020.3.19
OS: Windows 10

Re: Combine string in setprop?

Postby Octal450 » Sat May 13, 2017 5:55 pm

Thank you!

Kind Regards,
Josh
Skillset: JSBsim Flight Dynamics, Systems, Canvas, Autoflight/Control, Instrumentation, Animations
Aircraft: A320-family, MD-11, MD-80, Contribs in a few others

Octal450's GitHub|Launcher Catalog
|Airbus Dev Discord|Octal450 Hangar Dev Discord
User avatar
Octal450
 
Posts: 5583
Joined: Tue Oct 06, 2015 1:51 pm
Location: Huntsville, AL
Callsign: WTF411
Version: next
OS: Windows 11

Re: Combine string in setprop?

Postby Octal450 » Sat May 13, 2017 6: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.
Skillset: JSBsim Flight Dynamics, Systems, Canvas, Autoflight/Control, Instrumentation, Animations
Aircraft: A320-family, MD-11, MD-80, Contribs in a few others

Octal450's GitHub|Launcher Catalog
|Airbus Dev Discord|Octal450 Hangar Dev Discord
User avatar
Octal450
 
Posts: 5583
Joined: Tue Oct 06, 2015 1:51 pm
Location: Huntsville, AL
Callsign: WTF411
Version: next
OS: Windows 11

Re: Combine string in setprop?

Postby Necolatis » Sat May 13, 2017 6: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
User avatar
Necolatis
 
Posts: 2233
Joined: Mon Oct 29, 2012 1:40 am
Location: EKOD
Callsign: Leto
IRC name: Neco
Version: 2020.3.19
OS: Windows 10

Re: Combine string in setprop?

Postby Octal450 » Sat May 13, 2017 6:19 pm

OK will do.

Thanks again,
Josh
Skillset: JSBsim Flight Dynamics, Systems, Canvas, Autoflight/Control, Instrumentation, Animations
Aircraft: A320-family, MD-11, MD-80, Contribs in a few others

Octal450's GitHub|Launcher Catalog
|Airbus Dev Discord|Octal450 Hangar Dev Discord
User avatar
Octal450
 
Posts: 5583
Joined: Tue Oct 06, 2015 1:51 pm
Location: Huntsville, AL
Callsign: WTF411
Version: next
OS: Windows 11

Re: Combine string in setprop?

Postby Octal450 » Sun May 14, 2017 6: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?
Skillset: JSBsim Flight Dynamics, Systems, Canvas, Autoflight/Control, Instrumentation, Animations
Aircraft: A320-family, MD-11, MD-80, Contribs in a few others

Octal450's GitHub|Launcher Catalog
|Airbus Dev Discord|Octal450 Hangar Dev Discord
User avatar
Octal450
 
Posts: 5583
Joined: Tue Oct 06, 2015 1:51 pm
Location: Huntsville, AL
Callsign: WTF411
Version: next
OS: Windows 11

Re: Combine string in setprop?

Postby jam007 » Sun May 14, 2017 7: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: 579
Joined: Sun Dec 16, 2012 11:04 am
Location: Uppsala, Sweden
Callsign: LOOP
Version: 2020.4.0
OS: Ubuntu 22.04

Re: Combine string in setprop?

Postby Octal450 » Mon May 15, 2017 7: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));
      }
   }
Skillset: JSBsim Flight Dynamics, Systems, Canvas, Autoflight/Control, Instrumentation, Animations
Aircraft: A320-family, MD-11, MD-80, Contribs in a few others

Octal450's GitHub|Launcher Catalog
|Airbus Dev Discord|Octal450 Hangar Dev Discord
User avatar
Octal450
 
Posts: 5583
Joined: Tue Oct 06, 2015 1:51 pm
Location: Huntsville, AL
Callsign: WTF411
Version: next
OS: Windows 11

Re: Manipulating Strings with Nasal

Postby Octal450 » Mon May 15, 2017 11: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
Skillset: JSBsim Flight Dynamics, Systems, Canvas, Autoflight/Control, Instrumentation, Animations
Aircraft: A320-family, MD-11, MD-80, Contribs in a few others

Octal450's GitHub|Launcher Catalog
|Airbus Dev Discord|Octal450 Hangar Dev Discord
User avatar
Octal450
 
Posts: 5583
Joined: Tue Oct 06, 2015 1:51 pm
Location: Huntsville, AL
Callsign: WTF411
Version: next
OS: Windows 11

Re: Manipulating Strings with Nasal

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

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

Re: Manipulating Strings with Nasal

Postby Octal450 » Tue May 16, 2017 2:40 am

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

Kind Regards,
Josh
Skillset: JSBsim Flight Dynamics, Systems, Canvas, Autoflight/Control, Instrumentation, Animations
Aircraft: A320-family, MD-11, MD-80, Contribs in a few others

Octal450's GitHub|Launcher Catalog
|Airbus Dev Discord|Octal450 Hangar Dev Discord
User avatar
Octal450
 
Posts: 5583
Joined: Tue Oct 06, 2015 1:51 pm
Location: Huntsville, AL
Callsign: WTF411
Version: next
OS: Windows 11

Re: Manipulating Strings with Nasal

Postby Octal450 » Thu May 18, 2017 9: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
Skillset: JSBsim Flight Dynamics, Systems, Canvas, Autoflight/Control, Instrumentation, Animations
Aircraft: A320-family, MD-11, MD-80, Contribs in a few others

Octal450's GitHub|Launcher Catalog
|Airbus Dev Discord|Octal450 Hangar Dev Discord
User avatar
Octal450
 
Posts: 5583
Joined: Tue Oct 06, 2015 1:51 pm
Location: Huntsville, AL
Callsign: WTF411
Version: next
OS: Windows 11

Next

Return to Nasal

Who is online

Users browsing this forum: No registered users and 2 guests