Source language: Translate to:

SearchStr and StrReplace

General questions about NeoBook

Moderator: Neosoft Support

SearchStr and StrReplace

Postby StarterPack » Sat Mar 11, 2017 5:32 am

Hi guys, I'm building a program with variety beats and tempo.
What I want to know is how do I Search for multiple strings in 1 Script line

Example: The tempo starts from 40bpm to 140bpm, so I want the script to search from 40 to 140

What I have is

Code: Select all
SearchStr "40" "[PlayBeat]" "[Tempo]" ""
SearchStr "45" "[PlayBeat]" "[Tempo]" ""
SearchStr "50" "[PlayBeat]" "[Tempo]" ""

till
Code: Select all
SearchStr "140" "[PlayBeat]" "[Tempo]" ""


I want to use only 1 Script line to search from 40 to 140

and also when I use the StrReplace command

I want the script to search for the tempo and replace it with ""

So now I have to add multiple lines again

Example:
Code: Select all
StrReplace "[Playbeat]" " 40" "" "[PlayBeat]" ""
StrReplace "[Playbeat]" " 45" "" "[PlayBeat]" ""
StrReplace "[Playbeat]" " 50" "" "[PlayBeat]" ""

till
Code: Select all
StrReplace "[Playbeat]" " 140" "" "[PlayBeat]" ""


I want it to search for numbers between 40 and 140 and if it finds it, it must replace it with [String]

Hope i explained it right

Thank You
StarterPack
 
Posts: 115
Joined: Fri Feb 19, 2016 6:55 am

Re: SearchStr and StrReplace

Postby Gaev » Sat Mar 11, 2017 7:11 am

StarterPack:

Not sure you can do it in one line/command ... but you could use a Loop/EndLoop code block ...
Code: Select all
Loop "8" "28" "[thisLoop]"
  SetVar "[thisBeat]" "5*[thisLoop]"
  SearchStr "[thisBeat]" "[PlayBeat]" "[Tempo]" ""
EndLoop


Can you post an example (or two) of what [PlayBeat] looks like ?
Last edited by Gaev on Sat Mar 11, 2017 5:12 pm, edited 1 time in total.
User avatar
Gaev
 
Posts: 3718
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Re: SearchStr and StrReplace

Postby StarterPack » Sat Mar 11, 2017 12:17 pm

Hi Gaev,

Ok, I have a list of drum beats in ListBox
60's Style1 Sidestick
60's Style2 Sidestick etc
Tempo is by default 100bpm
When i double click on one of the styles in ListBox it will load string to variable (MainBeat)

Code: Select all
SetVar "[MainBeat]" ""
StrIns "[ListBox]" "[MainBeat]" "" "[MainBeat]"
SetVar "[PlayBeat]" "[MainBeat] [Tempo]bpm"


So let's say I double click 60's Style1 Sidestick, it will load [PlayBeat] with
60's Style1 Sidestick 100bpm

From here I can change the tempo up or down
Code: Select all
Math "[Tempo]+5" "" "[Tempo]"
SetVar "[PlayBeat]" ""
StrIns "[MainBeat] [Tempo]bpm" "[PlayBeat]" "" "[PlayBeat]"


Now if I have a tempo of 130bpm
60's Style1 Sidestick 130bpm

I can save it in my Favourite List (Favourite)
Code: Select all
SetVar "[Favourite]" "[PlayBeat]"


So......

when i double click on 60's Style1 Sidestick 130bpm in Favourite List, it must load 60's Style1 Sidestick in [PlayBeat]
and 130 in {Tempo]
StarterPack
 
Posts: 115
Joined: Fri Feb 19, 2016 6:55 am

Re: SearchStr and StrReplace

Postby Gaev » Sat Mar 11, 2017 5:37 pm

StarterPack:

Now if I have a tempo of 130bpm
60's Style1 Sidestick 130bpm

when i double click on 60's Style1 Sidestick 130bpm in Favourite List, it must load 60's Style1 Sidestick in [PlayBeat]
and 130 in [Tempo]

If you were to compose your composite string slightly differently e.g. ...
Code: Select all
60's Style1 Sidestick bpm=130
OR
60's Style1 Sidestick - 130bpm

... then it would be very easy to separate the [PlayBeat] and [Tempo] components using StrParse e.g. ...
Code: Select all
StrParse "[Favourite]" " bpm=" "[FavouritePart]" "[junk]"
SetVar "[PlayBeat]" "[FavouritePart1]"
SetVar "[Tempo]" "[FavouritePart2]"
OR
StrParse "[Favourite]" " - " "[FavouritePart]" "[junk]"
SetVar "[PlayBeat]" "[FavouritePart1]"
StrReplace "[FavouritePart2]" "bpm" "" "[Tempo]" ""


But if you insist on using your current format you can still do it like so ...
Code: Select all
StrParse "[Favourite]" " " "[FavouritePart]" "[PartCount]"
SetVar "[TempoPart]" "[FavouritePart[PartCount]]"
StrReplace "[Favourite]" " [TempoPart]" "" "[PlayBeat]" ""
StrReplace "[TempoPart]" "bpm" "" "[Tempo]" ""


Warning - none of the above code has been tested/verified by me ... so beware typo errors !!!
User avatar
Gaev
 
Posts: 3718
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Re: SearchStr and StrReplace

Postby StarterPack » Sat Mar 11, 2017 9:58 pm

But if you insist on using your current format you can still do it like so ...


Wow! Thank you Gaev, this works perfectly for me.
StarterPack
 
Posts: 115
Joined: Fri Feb 19, 2016 6:55 am

Re: SearchStr and StrReplace

Postby mishem » Sun Mar 12, 2017 12:55 am

For working with strings and would advise the wonderful plugin hpwControl.
And for advanced find and replace at least briefly to study regular expressions.
While using this plugin you can do without them. :)
mishem
 
Posts: 574
Joined: Mon Oct 08, 2012 1:51 pm

Re: SearchStr and StrReplace

Postby virger » Sun Mar 12, 2017 8:44 pm

A buttom:
Code: Select all
{NeoBook 5 Objects}
NeoBookVer=5,80
ObjectType=3
Name=BtRegExp
X=98
Y=223
W=477
H=47
Anchor=0
Text=[vtxt]¶[vret]
Align=1
ImageStyle=0
XPTheme=Yes
ObjAction=setvar "[vtxt]" "60's Style1 Sidestick 100bpm | 60's Style1 Sidestick 130bpm"¶setvar "[vres]" "bpm"¶setvar "[vrep]" "@"¶Call "RegExpRep" "[vtxt]" "[vres]" "[vrep]" "[vRet]"
LineColor=0
LineWidth=1
LineStyle=0
FillColor=12632256
FillPattern=0
Font=Arial
FontSize=10
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=14


A Function, 'JavaScript Type' name:'RegExpRep'

Code: Select all
{NeoBook Function}
Version=5,80
Language=JScript
Param=[%str]|Variable|Text To Search
Param=[%sea]|Variable|Text To Search
Param=[%rep]|Variable|Text For Replace
Param=[%ret]|Variable|Return Result
{End}
// Strina for Serach/Replace
var str = nbGetVar("[%str]");

// String To Search
var sea = nbGetVar("[%sea]");

// String To Replace
var rep = nbGetVar("[%rep]");

// Create a TXT Var
var txt = "([0-9]+"+sea+")";

// Create a 'RegExp Object'
var reg = new RegExp(txt,"gi");

nbSetVar("[%ret]", str.replace(reg, rep ));


Tkx For ReadMe
COSTA RICA
PURA VIDA
User avatar
virger
 
Posts: 510
Joined: Mon Sep 18, 2006 12:21 pm
Location: Costa Rica, America Central


Return to General NeoBook Discussions

Who is online

Users browsing this forum: No registered users and 1 guest