How do I make a function with if in Kontakt? - audio

I make a function that contains if in NI Kontakt:
on init
message(Add(1,2))
end on
function Add(x,y) -> output
if x > 0
output := x + y
else
output := 0
end if
end function
And I get the error message:
The definition of function Add needs to consist of a single line (eg.
"result := ") in order to be used in this context
Ho do I make a function with if?

there are several things wrong. I'm sure reading some example code helps to avoid too much try-and-error with this exotic language. But that's probably done after almost 4 months? ;-)
Firstly you need to declare all variables in the on init and always use their corresponding prefix (for integers its "$") like so:
on init
declare $x
declare $y
declare $output
end on
Secondly you can not call a function in the on init. For this example I use the on note callback that triggers every time you play a note. Additionally use "call" to execute a function.
on note
$x := 1
$y := 2
call Add
message($output)
end on
And lastly use brackets around your conditions:
function Add
if ($x > 0)
$output := $x + $y
else
$output := 0
end if
end function
It is like in most programming languages important to declare all your functions before their execution. Since you can not use them in the on init, you can place this callback always on top followed by your functions.
This would be the full code:
on init
declare $x
declare $y
declare $output
end on
function Add
if ($x > 0)
$output := $x + $y
else
$output := 0
end if
end function
on note
$x := 1
$y := 2
call Add
message($output)
end on
Enjoy ;-)

Related

How to iterate over a series of numbers passed as arguments to a bash function and then return a count of even numbers?

#this function checks if a number is even
#checks only 1 argument
function isiteven {
if [[ $1%2 -eq 0 ]]
then
echo "1"
else
echo "0"
fi
}
I need to use this file as source to another file and create a function that returns the count of even numbers passed to a function called 'nevens'.
I tried this code:
source program6.sh
function nevens {
for check in $#
do
if [[ -e isiteven$# ]]
then
let count=count+1
fi
done
echo $count
}
I am confused what shell sign to use to iterate in for loop and to check in if statement.
Since you're outputting a 0 or a 1 from isiteven, you can just unconditionally add the output of the function:
#!/bin/bash
source program6.sh
function nevens {
local count=0
for check; do # implicit `for check in "$#"`
(( count += $(isiteven $check) )) # add output of `isiteven $check`
done
echo $count
}
This assumes that your input is all valid, so if that's not guaranteed to be the case, you will need to add some checks.
Note that this script uses several non-standard features that won't work in all shells:
source program6.sh instead of the standard . program6.sh
function keyword instead of just nevens () {
local keyword to declare a local variable inside the function
+=, instead of the standard count=$(( count + $(isiteven $check) ))

convert 0 into string

i'm working on a script in perl.
This script read a DB and generate config file for other devices.
I have a problem with "0".
From my database, i get a 0 (int) and i want this 0 become a "0" in the config file. When i get any other value (1,2,3, etc), the script generate ("1","2","3", etc). But the 0 become an empty string "".
I know, for perl:
- undef
- 0
- ""
- "0"
are false.
How can i convert a 0 to "0" ? I try qw,qq,sprintf, $x = $x || 0, and many many more solutions.
I juste want to make a explicit conversion instead of an implicite conversion.
Thank you for your help.
If you think you have zero, but the program thinks you have an empty string, you are probably dealing with a dualvar. A dualvar is a scalar that contains both a string and a number. Perl usually returns a dualvar when it needs to return false.
For example,
$ perl -we'my $x = 0; my $y = $x + 1; CORE::say "x=$x"'
x=0
$ perl -we'my $x = ""; my $y = $x + 1; CORE::say "x=$x"'
Argument "" isn't numeric in addition (+) at -e line 1.
x=
$ perl -we'my $x = !1; my $y = $x + 1; CORE::say "x=$x"'
x=
As you can see, the value returned by !1 acts as zero when used as a number, and acts as an empty string when used as a string.
To convert this dualvar into a number (leaving other numbers unchanged), you can use the following:
$x ||= 0;

Is there a syntax error in this Bash function? Functionality works fine when hardcoded into main loop

I have a function that assigns an element of workspace[i] to a corrosponding "tag." For example, if workspace[3] has the name "firefox," it will underline the name "firefox," and if not, there will be no underline. Outside of the function, and inside the main while loop, this works perfectly, as such:
# dummy assignment for workspace_raw[i]
workspace_raw[1]=firefox
# code without function
boolean_check=false
for (( i=0 ; i <= 15 ; i++ ))
do
workspaces[i]="${workspace_raw[i]}"
if [ "${workspaces[i]}" == "firefox" ]
then
boolean_check=true
else
false
fi
done
if $boolean_check
then
workspace_firefox="\u3firefox\u0"
else
workspace_firefox="\u0firefox\u0"
fi
However, putting the functionality inside of a function and calling it with an argument does absolutely nothing, but doesn't display an error either. This is the code I'm using with the function, and it doesn't work:
# dummy assignment for $workspace_raw[i]
workspace_raw[1]=firefox
# assign name for function argument
tag_name1=firefox
# the function
assign_workspace()
{
tag=$1
boolean_check=false
for (( i=0 ; i <= 15 ; i++ ))
do
workspaces[i]="${workspace_raw[i]}"
if [ "${workspaces[i]}" == "$tag" ]
then
boolean_check=true
else
false
fi
done
if $boolean_check
then
declare workspace_$tag="\u3test$tag\u0"
else
declare workspace_$tag="\u0test$tag\u0"
fi
}
# call function with argument
assign_workspace $tag_name1
No idea what to do. Since the function's code works when hardcoded into the main loop, I feel like I'm making a syntax error somewhere in the code. Here is the entire script if necessary:
http://pastebin.com/39WmPUvi
Thanks for the help!
declare inside a function, by default, will create a local variable. If you want the workspace_$tag variables available outside that function, use the -g flag.
if $boolean_check
then
declare -g workspace_$tag="\u3test$tag\u0"
else
declare -g workspace_$tag="\u0test$tag\u0"
fi

pass variables to a function in bash(weird output?)

function f(){
local y=$1;
local z=$2;
echo $x $y $z;
}
function main(){
x=1;
y=2;
z=3;
f $y $z;
}
main $*
My output is 1 2 3
Why does this happen? I only passed two variables y and z.
$1 would be y from main which is 2 so back in function f local y=$1 would be y=2. The same thing for local z=$2, it would be z=3.
So I would assume either an error because I'm trying to echo $x which isn't a valid variable or I should get 2 3.
Why does it echo out 1 2 3?
$x is a global variable. The fact that it is assigned within a function doesn't change that.
So of course it is visible in f() or any other function.

How do you pass in parameters to the Inno Setup command line compiler?

It was suggested in the IS newsgroup to use /D= but using the iscc.exe that came with version 5.2.3 I get an "Unknown option:" error.
Then in the script, how do you use the value of the command line parameter?
You do, as MicSim says, need the preprocessor. It's included in the latest ISPack. Once it's installed, iscc supports /D.
You can then use the values defined like this (assuming you'd done /DVERSION_NAME=1.23):
AppVerName=MyApplication v{#VERSION_NAME}
From the Inno Setup helpfile:
Inno Setup Preprocessor replaces the
standard Inno Setup Command Line
Compiler (ISCC.exe) by an extended
version. This extended version
provides extra parameters to control
Inno Setup Preprocessor.
The "extra parameters" include the /d option.
The point of the answer by #Steven Dunn is to solve the problem with another layer of abstraction: instead of calling iscc your_script.iss directly from the terminal, call your_script.ps1 -YourVar "value", process the switch, write a #define to the .iss file, and then compile it with iscc. This was not articulated well and I don't think the function shown to parse command line arguments added much value. However, I'm giving credit where credit is due.
As #jdigital mentioned, ISPP has the /d switch, but ISPP can't be run directly from the terminal (AFAIK). Hence, something like a secondary scripted approach hinted at by #Steven Dunn is necessary.
You can achieve this by adding placeholders to an existing .iss script and then overwrite them accordingly:
.iss Template
; -- template.iss --
#define MyVar ""
...
.ps1 Script
#requires -PSEdition Core
# create_iss_script.ps1
param(
[Parameter(Mandatory=$true)][String]$MyVar,
[Parameter(Mandatory=$true)][String]$OutFile,
)
$File = '.\template.iss'
$LineToReplace = '#define MyVar ""'
$NewLine = "#define MyVar ""${MyVar}"""
$FileContent = Get-Content -Path $File -Raw
$FileContent.Replace($LineToReplace, $NewLine) | Set-Content -Path $OutFile
Example Terminal Usage
PS> .\create_iss_script.ps1 -MyVar "HelloWorld!" -OutFile "helloworld.iss"
PS> iscc .\helloworld.iss
or run the iscc step from within your .ps1 script, if you prefer.
If you want to parse command line arguments from code in inno, then use a method similar to this. Just call the inno script from the command line as follows:
C:\MyInstallDirectory>MyInnoSetup.exe -myParam parameterValue
Then you can call the GetCommandLineParam like this wherever you need it:
myVariable := GetCommandLineParam('-myParam');
//==================================================================
{ Allows for standard command line parsing assuming a key/value organization }
function GetCommandlineParam (inParam: String):String;
var
LoopVar : Integer;
BreakLoop : Boolean;
begin
{ Init the variable to known values }
LoopVar :=0;
Result := '';
BreakLoop := False;
{ Loop through the passed in arry to find the parameter }
while ( (LoopVar < ParamCount) and
(not BreakLoop) ) do
begin
{ Determine if the looked for parameter is the next value }
if ( (ParamStr(LoopVar) = inParam) and
( (LoopVar+1) < ParamCount )) then
begin
{ Set the return result equal to the next command line parameter }
Result := ParamStr(LoopVar+1);
{ Break the loop }
BreakLoop := True;
end
{ Increment the loop variable }
LoopVar := LoopVar + 1;
end;
end;
Hope this helps...

Resources