Page 1 of 2 12>
Topic Options
#179580 - 2007-08-22 05:16 PM Golf Code...
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
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
_________________________
Today is the tomorrow you worried about yesterday.

Top
#179581 - 2007-08-22 05:23 PM Re: Golf Code... [Re: Gargoyle]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
$Array = "Site","Loc","A","Type"
this only works when putting in more than one item

Top
#179583 - 2007-08-22 05:37 PM Re: Golf Code... [Re: Witto]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
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?
_________________________
Today is the tomorrow you worried about yesterday.

Top
#179584 - 2007-08-22 05:38 PM Re: Golf Code... [Re: Gargoyle]
Björn Offline
Korg Regular
*****

Registered: 2005-12-07
Posts: 953
Loc: Stockholm, Sweden.
 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


Edited by Björn (2007-08-22 05:43 PM)
_________________________
as long as it works - why fix it?
If it doesn't work - kix-it!

Top
#179585 - 2007-08-22 05:45 PM Re: Golf Code... [Re: Björn]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
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.
_________________________
Today is the tomorrow you worried about yesterday.

Top
#179586 - 2007-08-22 06:09 PM Re: Golf Code... [Re: Gargoyle]
Björn Offline
Korg Regular
*****

Registered: 2005-12-07
Posts: 953
Loc: Stockholm, Sweden.
no idea regarding how to use the split as part of the if right now.. (prolly never \:\) )
_________________________
as long as it works - why fix it?
If it doesn't work - kix-it!

Top
#179588 - 2007-08-22 06:20 PM Re: Golf Code... [Re: Björn]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
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.
_________________________
Today is the tomorrow you worried about yesterday.

Top
#179603 - 2007-08-22 10:22 PM Re: Golf Code... [Re: Gargoyle]
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
 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.


Edited by Howard Bullock (2007-08-22 10:23 PM)
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#179608 - 2007-08-22 10:43 PM Re: Golf Code... [Re: Howard Bullock]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
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"
_________________________
Today is the tomorrow you worried about yesterday.

Top
#179610 - 2007-08-22 10:45 PM Re: Golf Code... [Re: Gargoyle]
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
works either way.

"A" = split or split ="A"
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#179611 - 2007-08-22 10:47 PM Re: Golf Code... [Re: Howard Bullock]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
Thanks learned something new today - Never realized that one could reference the element of a Split as part of the Split function.
_________________________
Today is the tomorrow you worried about yesterday.

Top
#179618 - 2007-08-22 11:16 PM Re: Golf Code... [Re: Gargoyle]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
 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)

Top
#179619 - 2007-08-22 11:34 PM Re: Golf Code... [Re: Witto]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
Sorry but $input changes depending on when the function is called in the script.
_________________________
Today is the tomorrow you worried about yesterday.

Top
#179624 - 2007-08-23 07:49 AM Re: Golf Code... [Re: Howard Bullock]
Björn Offline
Korg Regular
*****

Registered: 2005-12-07
Posts: 953
Loc: Stockholm, Sweden.
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...
_________________________
as long as it works - why fix it?
If it doesn't work - kix-it!

Top
#179628 - 2007-08-23 10:42 AM Re: Golf Code... [Re: Björn]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
It is on page 79 of the kix2010.doc you youngster , IIF only returns values.
Top
#179629 - 2007-08-23 12:09 PM Re: Golf Code... [Re: Witto]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
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
_________________________
Actually I am a Rocket Scientist! \:D

Top
#179630 - 2007-08-23 03:40 PM Re: Golf Code... [Re: Glenn Barnas]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
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?
_________________________
Today is the tomorrow you worried about yesterday.

Top
#179631 - 2007-08-23 03:43 PM Re: Golf Code... [Re: Gargoyle]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Yes, that's right!

G-
_________________________
Actually I am a Rocket Scientist! \:D

Top
#179632 - 2007-08-23 03:46 PM Re: Golf Code... [Re: Gargoyle]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
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]

Top
#179638 - 2007-08-23 06:09 PM Re: Golf Code... [Re: Witto]
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
 Code:
if  IIf(Split($String, '-')[2] = 'A', 1, 0)
   ;true
else
   ;false
endif



Edited by Bryce (2007-08-23 06:10 PM)

Top
Page 1 of 2 12>


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
2 registered (morganw, mole) and 415 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.145 seconds in which 0.107 seconds were spent on a total of 15 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org