Gargoyle
(MM club member)
2007-08-22 05:16 PM
Golf Code...

Okay I know that there has to be a better way to write this, but can't see it.

 Code:
$Input = "Site-Loc-A-Type"
$Array = Split($Input,"-")
If $Array[2] = "A"
 Do stuff
Else
 Do other stuff
EndIf


Witto
(MM club member)
2007-08-22 05:23 PM
Re: Golf Code...

$Array = "Site","Loc","A","Type"
this only works when putting in more than one item


Gargoyle
(MM club member)
2007-08-22 05:37 PM
Re: Golf Code...

You can assume that this is a sub procedure where the $Input will change each time, this is just a sample case.

 Code:
$input = "Site-Loc-A-Type"

Function Decide($Input)
 $Array = Split($Input,"-")
   If $Array[2] = "A"
    Do stuff
   Else
    Do other stuff
   EndIf
EndFunction


Or do I misunderstand your question?


Björn
(Korg Regular)
2007-08-22 05:38 PM
Re: Golf Code...

 Code:
$Input = "Site-Loc-A-Type"
$Array = Split($Input,"-")
IIf($Array[2]= "A","Do stuff","do other")


Or no?

 Code:
Function Decide($Input,$searchitem)
 $Array = Split($Input,"-")
IIf($Array[2]= $searchitem,"Do stuff","do other")
EndFunction

feels a bit to static for me.

btw, did I get any golfing done there? \:D


Gargoyle
(MM club member)
2007-08-22 05:45 PM
Re: Golf Code...

So there is not a way to put the SPLIT as part of the IF.

And IIF is a great way to help the golf. Had forgot about that one.


Björn
(Korg Regular)
2007-08-22 06:09 PM
Re: Golf Code...

no idea regarding how to use the split as part of the if right now.. (prolly never \:\) )

Gargoyle
(MM club member)
2007-08-22 06:20 PM
Re: Golf Code...

We all know that Jooel will come up with something that just looks foriegn to the rest of us. \:\) Still the IIF makes it much cleaner I think.

Howard Bullock
(KiX Supporter)
2007-08-22 10:22 PM
Re: Golf Code...

 Code:
$Input = "Site-Loc-A-Type"

If "A" = split($Input,"-")[2]
 ? "Do stuff"
Else
 ? "Do other stuff"
EndIf
 


IIF is only good to return a value not to perform multiple steps of code.


Gargoyle
(MM club member)
2007-08-22 10:43 PM
Re: Golf Code...

Howard,
This is exactly what I was unsure of being able to do..

 Quote:

split($Input,"-")[2]


I assume that one must write in reverse to make it work..
this would not work would it?
 Code:
If split($input,"-")[2] = "A"


Howard Bullock
(KiX Supporter)
2007-08-22 10:45 PM
Re: Golf Code...

works either way.

"A" = split or split ="A"


Gargoyle
(MM club member)
2007-08-22 10:47 PM
Re: Golf Code...

Thanks learned something new today - Never realized that one could reference the element of a Split as part of the Split function.

Witto
(MM club member)
2007-08-22 11:16 PM
Re: Golf Code...

 Originally Posted By: Gargoyle
Or do I misunderstand your question?

I think so

 Code:
$Input = "Site-Loc-A-Type"
$Array = Split($Input,"-")

score 48 (quick count)

golfed equals to
 Code:
$Array = "Site","Loc","A","Type"

score 30 (quick count)


Gargoyle
(MM club member)
2007-08-22 11:34 PM
Re: Golf Code...

Sorry but $input changes depending on when the function is called in the script.

Björn
(Korg Regular)
2007-08-23 07:49 AM
Re: Golf Code...

Howwie - so giving a variable a value with iif is out of the scope? don't quite follow acctually. That was the first time I even tried to apply IIF...

Witto
(MM club member)
2007-08-23 10:42 AM
Re: Golf Code...

It is on page 79 of the kix2010.doc you youngster , IIF only returns values.

Glenn BarnasAdministrator
(KiX Supporter)
2007-08-23 12:09 PM
Re: Golf Code...

IIf is cool, but realize that it has 2 limitations. Besides only returning values, it generally only compares values. You can't put a UDF or most other functions in the test part. Otherwise you could golf it down to
$x = IIf(Split($String, '-')[2] = 'A', 'True', 'False')
(which might work, actualy - just an example of what you really need to test)

One of my favorite uses for IIf is flag setting when there are multiple conditions in a long cycle of code that can set the flag. Here's some pseudo code:
 Code:
For Each $X in $Array
  $Flag = 0
 ; process $X
  $Flag = IIf($X = 3, 1, $Flag)
 ; process $X some more
  $Flag = IIf($X = 27, 1, $Flag)
 ; process $X yet again
  $Flag = IIf($X < 0, 1, $Flag)
  If $Flag "message" EndIf
Next

In this example, there are 3 conditions in each iteration where the flag can be set. Note that IIf either sets the flag to one, or to the current value of flag. This allows any test to set the flag, but no test can reset it once set until the loop iteration is complete.

This is really handy in checking for non-fatal errors, where you want to discard data but not terminate the script.

Glenn


Gargoyle
(MM club member)
2007-08-23 03:40 PM
Re: Golf Code...

So with IIF one could test for any T/F condition and the return could be anything you specify?

 Code:
 $Function = IIF($Array[$Count] = "A", "Master","Slave")


Would result in $Function becoming either Master or Slave?


Glenn BarnasAdministrator
(KiX Supporter)
2007-08-23 03:43 PM
Re: Golf Code...

Yes, that's right!

G-


Witto
(MM club member)
2007-08-23 03:46 PM
Re: Golf Code...

Like Glenn said, the variable $Function will get or the value "Master", or the value "Slave".
[Edit]
Why am I always so slow hitting that submit button
[/Edit]


Bryce
(KiX Supporter)
2007-08-23 06:09 PM
Re: Golf Code...

 Code:
if  IIf(Split($String, '-')[2] = 'A', 1, 0)
   ;true
else
   ;false
endif



LonkeroAdministrator
(KiX Master Guru)
2007-08-23 11:35 PM
Re: Golf Code...

iif() doesn't make any sense in this simple task.
it hardly ever does.


Richard H.Administrator
(KiX Supporter)
2007-08-24 11:20 AM
Re: Golf Code...

 Originally Posted By: Glenn
You can't put a UDF or most other functions in the test part.


Yes you can. Not to sure why you think that you cannot - do you have an example?

The tricky thing about IIf() is that BOTH the True/False sections are ALWAYS evaluated so you need to be very careful what you put there.

Here is a very simple example that illustrates a working scenario:
 Code:
$Var1=10
$Var2=5
 
$Action="Divide"
"Result of "+$Var1+" "+$Action+" "+$Var2+" is "+Iif($Action="Multiply",doMult($Var1,$Var2),doDiv($Var1,$Var2))+@CRLF
$Action="Multiply"
"Result of "+$Var1+" "+$Action+" "+$Var2+" is "+Iif($Action="Multiply",doMult($Var1,$Var2),doDiv($Var1,$Var2))+@CRLF
 
Function doMult($a,$b)
	$doMult=$a*$b
EndFunction
 
Function doDiv($a,$b)
	$doDiv=$a/$b
EndFunction


This works as you'd expect as even though both doMult() and doDiv() are called, the unused return value is simply discarded. This is dangerous though, as (in most languages) if $Var2 was zero you would get a "divide by zero" error even if the action was "Multiply". KiXtart is a bit more forgiving so you don't see the error.

Here is an example of when using functions causes an unexpected side effect:
 Code:
If 6=IIF($sType="New"
	,MessageBox("Thing does not exist - do you want to add it?","Add Item",4)
	,MessageBox("Thing already exists - do you want to change it?","Change Item",4)
	)
	"Ok, Thing is being actioned"+@CRLF
Else
	"Ok, you chose not to update Thing"+@CRLF
EndIf


If you run this then *both* MessageBox()'s will appear, though only the return value from the "Change Item" box will be used.


Witto
(MM club member)
2007-08-24 01:12 PM
Re: Golf Code...

So this is an "undocumented feature" or do I misunderstand the manual?

Richard H.Administrator
(KiX Supporter)
2007-08-24 01:42 PM
Re: Golf Code...

You misunderstand, but maybe I didn't explain it very well \:\)

IIF only returns values. However, the values may be derived from any source including complex expressions, the return values of functions, object methods and so-on.

The problem is the way that the parser / runtime resolves the symbols. Basically the expressions in the IIF() statement are resolved to simple values *before* the conditional part is evaluated.

Take one of the examples:
 Code:
$Var1=10
$Var2=2
$Action="Multiply"
"Result of "+$Var1+" "+$Action+" "+$Var2+" is "+Iif($Action="Multiply",doMult($Var1,$Var2),doDiv($Var1,$Var2))+@CRLF


The IIf() will resolve something like this:
  1. Iif($Action="Multiply",doMult($Var1,$Var2),doDiv($Var1,$Var2))
  2. Iif("Multiply"="Multiply",doMult(10,2),doDiv(10,2))
  3. Iif("Multiply"="Multiply",20,5)
  4. Iif(1,20,5)
  5. 20


That's not necessarily the exact process, but it's close enough to see what the problem is. What is clear is that the evaluation of the conditional part is the last thing that happens.

This causes two problems.
First, both the true and false expressions have been evaluated - in this case both functions have been called.
Second, it means that you cannot use *any* expression which might be invalid even if it is not going to be used. This includes simple variables as well as more complex expressions.

Here is a good example of how easy it is to fall into the trap. Let's say that you want to assign a default when a value has not been passed on the command line:
 Code:
$=SetOption("Explicit","ON")
$=IIf(IsDeclared($SERVER),$SERVER,".")


This looks fine - if the user has passed "$SERVER" on the command line then it is used. If the user has not specified $SERVER then the local computer "." is used instead.

The problem is that $SERVER is evaluated before the conditional, so the script will abort with an "undefined variable" error if the user has not passed the variable on the command line.

IMO IIf() in KiXtart is an accident waiting to happen, and is only really useful for golf. My recommendation is to avoid it in production work.


Witto
(MM club member)
2007-08-24 02:31 PM
Re: Golf Code...

$var = "Yes"
IIf($var = "Yes", CallYess(), CallNo())
Function CallYess()
   
"Yes" ?
   
$CallYess = "True"
EndFunction
Function CallNo()
   
"No" ?
   
$CallNo = "False"
EndFunction

OK, I see.
AFAIK "If...Then...Else...EndIf" does not have this problem.


Glenn BarnasAdministrator
(KiX Supporter)
2007-08-24 02:33 PM
Re: Golf Code...

Yes, you're right - this is what I was thinking of, but reversed my thoughts it as I wrote it.

Because both true and false functions are evaluated, it's easy to get into trouble with math functions - div/0 in particular.

Thanks for clearing that up!

Glenn


NTDOCAdministrator
(KiX Master)
2007-08-24 10:26 PM
Re: Golf Code...

You're just getting OLD there Glenn. As I recall you and I were some of the first to run into this issue. Got into conversation with Shawn and Jooel over it years ago as what looked like perfectly good code was giving the wrong response.

But true, unless it's very simple code you should probably avoid IIF in production.


Gargoyle
(MM club member)
2007-08-25 12:55 AM
Re: Golf Code...

If there are any wondering what my final result was...

 Code:
If "A" = Split($array[$c],"-")[2]


LonkeroAdministrator
(KiX Master Guru)
2007-08-25 04:27 PM
Re: Golf Code...

and without iif(), that's nice.
to really golf it, you would end up with:
 Code:
If Split($[$c],"-")[2] = A


Les
(KiX Master)
2007-08-25 04:36 PM
Re: Golf Code...

We could strip out the spaces and use single quotes too. :p
 Code:
If Split($[$c],'-')[2]=A


LonkeroAdministrator
(KiX Master Guru)
2007-08-25 06:19 PM
Re: Golf Code...

that wouldn't lower the golfscore though.
just would make it lonkenized, imo.


Les
(KiX Master)
2007-08-25 07:04 PM
Re: Golf Code...

It was a joke... like the Dilbert cartoon of the PHB wanting a lighter laptop and being told to delete files to make it lighter.

I don't know why this version of UBB left out the tongue-in-cheek emoticon. :p Perhaps you missed that?


LonkeroAdministrator
(KiX Master Guru)
2007-08-25 07:57 PM
Re: Golf Code...

indeed.

how did you know? you'r a psychic?