Use conditional if with condition in string

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
aengelriv
Posts: 10
Joined: Fri Sep 08, 2017 5:47 am

Use conditional if with condition in string

Post by aengelriv »

greetings

I need to use 'if' conditions built by code
the condition in a string text
see example:

Code: Select all

Sub is_false_or_true_with_string_condition
'1) ''  with string
one="red" : two="red"   
    linecommand0=chr(34) & one & chr(34) & ""="" & chr(34) & two & chr(34)
    if linecommand0 then
   		 msgbox "Yes equal " & one
   	else
   	    msgbox "not equal"	 
    endif
  '      ------------- work ok  
'2) ''  with values
    one=6 : two=8 
    linecommand0=chr(34) & one & chr(34) & "">"" & chr(34) & two & chr(34)
    if linecommand0 then
   		 msgbox "is mayor " & one
   	else
   	    msgbox "is minor"	 
    endif
    '-------  work well

'3)   but if the operator is in variable ....  errorr... 
    part1=2 : part2=2 : operat="="
    comandline1= chr(34) & part1 & chr(34) &  chr(34) &  "" & operat &  "" & chr(34) &  chr(34) & part2 & chr(34)
     '''   here problem with variable operat << ---
    msgbox comandline1    
    if comandline1 then
      msgbox "yes"
    endif
 ' not work....  

end sub 
    
I need to place the operator in the variable because it changes according to the procedure.

How would the arrangement in the code.
thanks!
Last edited by aengelriv on Wed Mar 27, 2019 2:47 am, edited 1 time in total.
Llibreoffice 4.4 windows/Puppylinux
FJCC
Moderator
Posts: 9270
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Use condicional if in string

Post by FJCC »

Something like this?

Code: Select all

part1=2 : part2=2 : operat= "="

SELECT CASE operat
  Case "="
    comandline1 = (part1 = part2)
  Case ">"
    comandline1 = (part1 > part2)
  Case "<"
    comandline1 = (part1 < part2)
End Select
     
    if comandline1 then
      msgbox part1 & operat & part2
    else
      msgbox part1 & " not " & operat & part2
    endif
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
JeJe
Volunteer
Posts: 2777
Joined: Wed Mar 09, 2016 2:40 pm

Re: Use conditional if in string

Post by JeJe »

my oops.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2777
Joined: Wed Mar 09, 2016 2:40 pm

Re: Use conditional if in string

Post by JeJe »

Perhaps you could use an Eval function like this one? cbool(eval("2>1")), cbool(eval("2=2")) etc seems to work

http://ftp.planet-source-code.com/vb/sc ... 6&lngWId=1
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Lupp
Volunteer
Posts: 3548
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Use conditional if in string

Post by Lupp »

There aren't just three ordinary comparators, but 6. And sometimes you may want to parametrize different kinds of calculations.

The very different approach demonstrated in the attached document is supposed to look funny. You may want to see it nonetheless.
(You can create a FunctionAccess service independent of the type of the document for which the macro is called.)

Limitation: You can not call a user defined function via the FunctionAccess service.

By the way: The subject of this thread seems somehow misleading to me.
Attachments
aoo97502ParametrizedComparatorInUserCode_1.ods
(17.86 KiB) Downloaded 142 times
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
MrProgrammer
Moderator
Posts: 4901
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: Use conditional if with condition in string

Post by MrProgrammer »

aengelriv wrote:I need to use 'if' conditions built by code the condition in a string text … I need to place the operator in the variable because it changes according to the procedure.
Basic does not support that feature. The solution is to write your macro in Python and use eval().
aengelriv wrote:
chr(34) & one & chr(34) &           "" =          ""           & chr(34) & two & chr(34)
chr(34) & one & chr(34) &           "" >          ""           & chr(34) & two & chr(34)
chr(34) & one & chr(34) & chr(34) & "" & operat & "" & chr(34) & chr(34) & two & chr(34)
Your example is bogus because the third expression does not have the same form as the previous two. The first two have a comparison operator (= or >) between the empty strings ("") and thus they evaluate to TRUE or FALSE. The third has string concatenation between the empty strings and the expression evaluates to a string. However, any attempt like this is pointless because Basic does not support the feature you want.

If this solved your problem please go to your first post use the Edit button and add [Solved] to the start of the title. You can select the green checkmark icon at the same time.
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.6.3, iMac Intel.   The locale for any menus or Calc formulas in my posts is English (USA).
JeJe
Volunteer
Posts: 2777
Joined: Wed Mar 09, 2016 2:40 pm

Re: Use conditional if with condition in string

Post by JeJe »

MrProgrammer - did you look at the link I posted... there is very often an off-the-shelf VB6 solution to these sorts of problems that will run with no/a little adaptation to OO. Finding someone else's code that does what you want is way easier...
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
MrProgrammer
Moderator
Posts: 4901
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: Use conditional if with condition in string

Post by MrProgrammer »

JeJe wrote:did you look at the link I posted... there is very often an off-the-shelf VB6 solution …
However OpenOffice uses StarBasic, not VB6. I don't know what all the differences between the two might be, and I doubt the OP does either. Some tests show:
• Eval("2 * 3 ^ 2") is 36, but in StarBasic 2 * 3 ^ 2 is 18.
• Eval("(-1) Xor (0)") is 0 but in StarBasic (-1) Xor (0) is -1.
• With x=1, Eval("7+x") is 7 but in Starbasic 7+x is 8.

Rather than deal with these difficulties it seems better to use Python where an eval function is provided by the language.
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.6.3, iMac Intel.   The locale for any menus or Calc formulas in my posts is English (USA).
JeJe
Volunteer
Posts: 2777
Joined: Wed Mar 09, 2016 2:40 pm

Re: Use conditional if with condition in string

Post by JeJe »

MrProgrammer - I get identical results, both in OO and in VB6, giving

MsgBox Eval("2 * 3 ^ 2") ' 36
MsgBox Eval("(-1) Xor (0)") ' 0
MsgBox Eval("7+1") '8

MsgBox 2 * 3 ^ 2 ' 18
MsgBox (-1) Xor (0) '-1
x = 1
MsgBox 7 + x '8

The only difference is in OO I got an unexpected symbol error with (-1) and the brackets had to be removed.

Your examples are interesting though - that may not be the best eval function out there, there are several to choose from.
It works within the range required by the OP though.

There are strong advocates for Python here. If you already know it - then sure. If you don't then you may easily find a VB6 function that does what you want. And Basic has the IDE and some (if limited) crossover with VBA.

Edit:
Looking again at the function.

x=1, Eval("7+x") is never going to work in any function, python or basic. X is always going to equal 0 in the string.

MsgBox Eval("-1 xor 0") gives -1 the same as -1 xor 0
You've introduced brackets and that errors in OO anyway.

The difference with ^ is explained in the comments above the function
"exponentiation ^ has same level of multiplication and division"

That's the only real quibble here - it doesn't evaluate the power first... but that limitation is explained. With that limitation its a good function I think...
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply