Use conditional if with condition in string

Creating a macro - Writing a Script - Using the API

Use conditional if with condition in string

Postby aengelriv » Tue Mar 26, 2019 5:13 am

greetings

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

Code: Select all   Expand viewCollapse view
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
aengelriv
 
Posts: 7
Joined: Fri Sep 08, 2017 5:47 am

Re: Use condicional if in string

Postby FJCC » Tue Mar 26, 2019 5:41 am

Something like this?
Code: Select all   Expand viewCollapse view
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
Windows 10 and Linux Mint, since 2017
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 7204
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Use conditional if in string

Postby JeJe » Tue Mar 26, 2019 9:45 am

my oops.
Openoffice 4.1.2
Windows 8
JeJe
Volunteer
 
Posts: 523
Joined: Wed Mar 09, 2016 2:40 pm

Re: Use conditional if in string

Postby JeJe » Tue Mar 26, 2019 10:16 am

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
Openoffice 4.1.2
Windows 8
JeJe
Volunteer
 
Posts: 523
Joined: Wed Mar 09, 2016 2:40 pm

Re: Use conditional if in string

Postby Lupp » Tue Mar 26, 2019 12:45 pm

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 37 times
On Windows 10: LibreOffice 6.2 and older versions, PortableOpenOffice 4.1.5 and older, StarOffice 5.2
---
Lupp from München
User avatar
Lupp
Volunteer
 
Posts: 2491
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Use conditional if with condition in string

Postby MrProgrammer » Wed Mar 27, 2019 7:44 pm

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.5 Build 9789 on MacOS 10.11.6.   The locale for any menus or Calc formulas in my posts is English (USA).
User avatar
MrProgrammer
Moderator
 
Posts: 3747
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: Use conditional if with condition in string

Postby JeJe » Wed Mar 27, 2019 10:35 pm

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...
Openoffice 4.1.2
Windows 8
JeJe
Volunteer
 
Posts: 523
Joined: Wed Mar 09, 2016 2:40 pm

Re: Use conditional if with condition in string

Postby MrProgrammer » Mon Apr 01, 2019 5:36 pm

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.5 Build 9789 on MacOS 10.11.6.   The locale for any menus or Calc formulas in my posts is English (USA).
User avatar
MrProgrammer
Moderator
 
Posts: 3747
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: Use conditional if with condition in string

Postby JeJe » Mon Apr 01, 2019 7:17 pm

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...
Openoffice 4.1.2
Windows 8
JeJe
Volunteer
 
Posts: 523
Joined: Wed Mar 09, 2016 2:40 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 3 guests