Home All Groups Group Topic Archive Search About
Author
10 Sep 2010 5:04 PM
-mhd
Well at least it's a challenge for me :-)

What is an elegant way of referring to the first string item delimited by a space without
assigning to a string array variable as a separate step?

Normally you could do...
dim strArray() as string
MyStr = "1 2 3"
strArray = Split(MyStr)
Debug.Print strArray(0)

Debug.Print Split(MyStr,,1)   'return one sub string parameter naturally doesn't work

Why am I doing this?
MyStr is already used in a complicated IIF statement but I only want the first item in the
string.

While some may say that readability is an issue but many IIF statements in a row are
actually the readability factor in this particular code (just the stuff in the IIF may
require a second look).

Thanks!
-mhd

Author
10 Sep 2010 5:19 PM
Jim Mack
-mhd wrote:
> Well at least it's a challenge for me :-)
>
> What is an elegant way of referring to the first string item
> delimited by a space without
> assigning to a string array variable as a separate step?


MyStr = "1 2 3"
Debug.Print Split(MyStr)(0)

--
   Jim Mack
   Twisted tees at http://www.cafepress.com/2050inc
   "We sew confusion"
Author
10 Sep 2010 6:18 PM
-mhd
"Jim Mack" <no-uce-***@mdxi.com> wrote:

>MyStr = "1 2 3"
>Debug.Print Split(MyStr)(0)

Thanks!

-mhd
Author
10 Sep 2010 5:21 PM
David Kerber
In article <npnk86lm8balh9oon44vetbm3scv2fc***@4ax.com>,
not_r***@invalid.com says...
Show quoteHide quote
>
> Well at least it's a challenge for me :-)
>
> What is an elegant way of referring to the first string item delimited by a space without
> assigning to a string array variable as a separate step?
>
> Normally you could do...
> dim strArray() as string
> MyStr = "1 2 3"
> strArray = Split(MyStr)
> Debug.Print strArray(0)
>
> Debug.Print Split(MyStr,,1)   'return one sub string parameter naturally doesn't work
>
> Why am I doing this?
> MyStr is already used in a complicated IIF statement but I only want the first item in the
> string.
>
> While some may say that readability is an issue but many IIF statements in a row are
> actually the readability factor in this particular code (just the stuff in the IIF may
> require a second look).
>
> Thanks!
> -mhd

MsgBox Split(mystrStr, " ")(0)
Author
10 Sep 2010 6:19 PM
-mhd
David Kerber <dkerber@WarrenRogersAssociates.invalid> wrote:

>MsgBox Split(mystrStr, " ")(0)


Thanks!

-mhd
Author
10 Sep 2010 5:22 PM
dpb
-mhd wrote:
....
> What is an elegant way of referring to the first string item
> delimited by a space without assigning to a string array variable as
> a separate step?
....
> MyStr = "1 2 3"
....

Debug.Print Mid$(MyStr,1,InStr$(MyStr," ")-1)

Not terribly elegant, but one line.

....

> While some may say that readability is an issue but many IIF statements in a row are
> actually the readability factor in this particular code (just the stuff in the IIF may
> require a second look).
....

IIF() is, while compact, one of the most overhead intensive functions in
VB.  I'd recommend avoiding it like the plague in lieu of almost any
other construct.

--
Author
10 Sep 2010 6:16 PM
J.C.
> IIF() is, while compact, one of the most overhead intensive functions in
> VB.  I'd recommend avoiding it like the plague in lieu of almost any other
> construct.

Why avoid it? If it's only called a couple of times, what difference does it
make if it has a lot of overhead?  It's more important to write clear,
understandable code than to worry about code overhead. Worry about slow code
only when you have to.


Show quoteHide quote
"dpb" <n***@non.net> wrote in message
news:i6dpn2$p5b$1@news.eternal-september.org...
> -mhd wrote:
> ...
>> What is an elegant way of referring to the first string item
>> delimited by a space without assigning to a string array variable as
>> a separate step?
> ...
>> MyStr = "1 2 3"
> ...
>
> Debug.Print Mid$(MyStr,1,InStr$(MyStr," ")-1)
>
> Not terribly elegant, but one line.
>
> ...
>
>> While some may say that readability is an issue but many IIF statements
>> in a row are
>> actually the readability factor in this particular code (just the stuff
>> in the IIF may
>> require a second look).
> ...
>
> IIF() is, while compact, one of the most overhead intensive functions in
> VB.  I'd recommend avoiding it like the plague in lieu of almost any other
> construct.
>
> --
Author
10 Sep 2010 8:00 PM
dpb
J.C. wrote:
>> IIF() is, while compact, one of the most overhead intensive functions
>> in VB.  I'd recommend avoiding it like the plague in lieu of almost
>> any other construct.
>
> Why avoid it? If it's only called a couple of times, what difference
> does it make if it has a lot of overhead?  It's more important to write
> clear, understandable code than to worry about code overhead. Worry
> about slow code only when you have to.
....

OP says it's called often, first.

Second, why not code for efficiency when it is trivial to do so?

Third, it's also generally difficult to read (ie, clarity isn't enhanced
vis a vis the alternatives).

imo, $0.02, etc., etc, etc., ...

--
Author
11 Sep 2010 2:32 PM
-mhd
dpb <n***@non.net> wrote:

>J.C. wrote:
>>> IIF() is, while compact, one of the most overhead intensive functions
>>> in VB.  I'd recommend avoiding it like the plague in lieu of almost
>>> any other construct.
>>
>> Why avoid it? If it's only called a couple of times, what difference
>> does it make if it has a lot of overhead?  It's more important to write
>> clear, understandable code than to worry about code overhead. Worry
>> about slow code only when you have to.
>...
>
>OP says it's called often, first.

No I didn't.

-mhd
Author
10 Sep 2010 6:22 PM
-mhd
dpb <n***@non.net> wrote:

>Debug.Print Mid$(MyStr,1,InStr$(MyStr," ")-1)
>
>Not terribly elegant, but one line.

Thanks that works for me too!

-mhd
Author
11 Sep 2010 12:07 AM
argusy
dpb wrote:

<snip>

> IIF() is, while compact, one of the most overhead intensive functions in
> VB.  I'd recommend avoiding it like the plague in lieu of almost any
> other construct.
>
> --
Then why didn't you include the qualifier?

If IIF() is used occasionally, then who cares if it takes a poofteenth of a
microsecond longer (than what, btw)?

Even in a loop that iterates for only tens or even a few hundred times, it
probably makes no difference - would YOU (as a user) notice if a program took
milliseconds longer to run in 4 Gigabytes of RAM running at 3.3Ghz?

If an IFF() is used in a loop for thousands(millions?) of operations, and
compared to the same program using an IF..ENDIF,  _THEN_ I'd agree with you.

That's the qualifier -

IFF() is an overhead intensive function, _so in extremely loop intensive
situations it WILL increase program time compared to an IF.. ENDIF construct_

I don't really know how much overhead it uses, probably less than a Kb - but
even if it uses 100Kb, that's a red herring when there's 2, 4 or more
***GIGABYTES*** of memory to use.

That's the rub, mate. It's not the usage, it's the comparison to other
constructs that shows the difference.

I can only see the paranoia about time and overhead if a programmer is still
trying to reduce a program to fit into a SC/MP kit from the 60's. (Yep, I'm old
enough to know what it is, and if you, the reader, know what it is, it tells you
why I'm responding to this post).

I've seen responses to older posts arguing over how much time is taken by a
given statement or construct, but can only show the difference in time with
MILLIONS of loop cycles to prove their point

With modern PC's running with multi-core gigahertz processors having gigabytes
of memory at their disposal (but not ever using all of that), WHAT'S THE POINT
IN ARGUING OVER WHETHER A ONE LINE STATEMENT RUNS SLOWER THAN A FIVE LINE
STATEMENT, AND BY HOW MUCH.

Just accept it.
Author
11 Sep 2010 12:30 AM
dpb
argusy wrote:
> dpb wrote:
>
> <snip>
>
>> IIF() is, while compact, one of the most overhead intensive functions
>> in VB.  I'd recommend avoiding it like the plague in lieu of almost
>> any other construct.
>>
>> --
> Then why didn't you include the qualifier?
....

Becuz...rest of meaningless rant over nothing elided...

--
Author
11 Sep 2010 2:44 AM
argusy
dpb wrote:
Show quoteHide quote
> argusy wrote:
>> dpb wrote:
>>
>> <snip>
>>
>>> IIF() is, while compact, one of the most overhead intensive functions
>>> in VB.  I'd recommend avoiding it like the plague in lieu of almost
>>> any other construct.
>>>
>>> --
>> Then why didn't you include the qualifier?
> ...
>
> Becuz...rest of meaningless rant over nothing elided...
>
> --
Oh, thank you. Your reply says it all. No need to say any more
Graham
Author
11 Sep 2010 3:18 AM
dpb
argusy wrote:
Show quoteHide quote
> dpb wrote:
>> argusy wrote:
>>> dpb wrote:
>>>
>>> <snip>
>>>
>>>> IIF() is, while compact, one of the most overhead intensive
>>>> functions in VB.  I'd recommend avoiding it like the plague in lieu
>>>> of almost any other construct.
>>>>
>>>> --
>>> Then why didn't you include the qualifier?
>> ...
>>
>> Becuz...rest of meaningless rant over nothing elided...
>>
>> --
> Oh, thank you. Your reply says it all. No need to say any more
> Graham

Previously answered.

IIF() sucks big time both performance-wise and legibility vis a vis
alternatives.

--
Author
11 Sep 2010 4:16 AM
Tony Toews
On Fri, 10 Sep 2010 12:22:00 -0500, dpb <n***@non.net> wrote:

>IIF() is

I find that sooner or later it's going to come back and cause you
problems because both sides are evaluated.  

Tony

--
Tony Toews, Microsoft Access MVP
Tony's Main MS Access pages - http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
For a convenient utility to keep your users FEs and other files
  updated see http://www.autofeupdater.com/
Author
10 Sep 2010 6:06 PM
J.C.
Who cares about elegant code?  Your users don't care how elegant, cute, or
clever your code is. They just want a program that works.  I bet Microsoft
can create cute some code, but it always doesn't work, does it?


"-mhd" <not_r***@invalid.com> wrote in message
news:npnk86lm8balh9oon44vetbm3scv2fc9qa@4ax.com...
Well at least it's a challenge for me :-)

What is an elegant way of referring to the first string item delimited by a
space without
assigning to a string array variable as a separate step?

Normally you could do...
dim strArray() as string
MyStr = "1 2 3"
strArray = Split(MyStr)
Debug.Print strArray(0)

Debug.Print Split(MyStr,,1)   'return one sub string parameter naturally
doesn't work

Why am I doing this?
MyStr is already used in a complicated IIF statement but I only want the
first item in the
string.

While some may say that readability is an issue but many IIF statements in a
row are
actually the readability factor in this particular code (just the stuff in
the IIF may
require a second look).

Thanks!
-mhd
Author
10 Sep 2010 6:28 PM
-mhd
"J.C." <y***@nothanks.com> wrote:

>Who cares about elegant code?  Your users don't care how elegant, cute, or
>clever your code is. They just want a program that works.  I bet Microsoft
>can create cute some code, but it always doesn't work, does it?

For my own readability purpose I didn't want to break up a series of IIF statements with
an If Then ElseIf qualifier containing  a split assignment statement between them.

-mhd
Author
10 Sep 2010 6:41 PM
Phil Hunt
The one bad thing about IIF is that the "true part" and "false part" are
evaluated first before decision. Sometimes, one of the part would raise an
error.


"-mhd" <not_r***@invalid.com> wrote in message
news:mttk86572h5vqqfmtjbmmvu0dlmplci132@4ax.com...
"J.C." <y***@nothanks.com> wrote:

>Who cares about elegant code?  Your users don't care how elegant, cute, or
>clever your code is. They just want a program that works.  I bet Microsoft
>can create cute some code, but it always doesn't work, does it?

For my own readability purpose I didn't want to break up a series of IIF
statements with
an If Then ElseIf qualifier containing  a split assignment statement between
them.

-mhd
Author
10 Sep 2010 6:51 PM
-mhd
"Phil Hunt" <a**@aaa.com> wrote:

>The one bad thing about IIF is that the "true part" and "false part" are
>evaluated first before decision. Sometimes, one of the part would raise an
>error.

True but in this case it is safe.

-mhd
Author
10 Sep 2010 6:48 PM
J.C.
I was just making a generalized comment about programming. Code readability
and correctness is more important than cute--wow, would you look at
that!--code.

"-mhd" <not_r***@invalid.com> wrote in message
news:mttk86572h5vqqfmtjbmmvu0dlmplci132@4ax.com...
"J.C." <y***@nothanks.com> wrote:

>Who cares about elegant code?  Your users don't care how elegant, cute, or
>clever your code is. They just want a program that works.  I bet Microsoft
>can create cute some code, but it always doesn't work, does it?

For my own readability purpose I didn't want to break up a series of IIF
statements with
an If Then ElseIf qualifier containing  a split assignment statement between
them.

-mhd
Author
10 Sep 2010 9:32 PM
Tony Toews
On Fri, 10 Sep 2010 13:48:47 -0500, "J.C." <y***@nothanks.com> wrote:

>I was just making a generalized comment about programming. Code readability
>and correctness is more important than cute--wow, would you look at
>that!--code.

A while back I recall instructors stating that if you wrote the code
in a test in WatFiv or other program you would get partial marks if
only a few minor details wrong.

But if you wrote your solution in APL and it didn't work you didn't
get any marks for the answer. 

Tony
--
Tony Toews, Microsoft Access MVP
Tony's Main MS Access pages - http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
For a convenient utility to keep your users FEs and other files
  updated see http://www.autofeupdater.com/
Author
11 Sep 2010 12:07 AM
argusy
J.C. wrote:
Show quoteHide quote
> I was just making a generalized comment about programming. Code
> readability and correctness is more important than cute--wow, would you
> look at that!--code.
>
> "-mhd" <not_r***@invalid.com> wrote in message
> news:mttk86572h5vqqfmtjbmmvu0dlmplci132@4ax.com...
> "J.C." <y***@nothanks.com> wrote:
>
>> Who cares about elegant code?  Your users don't care how elegant,
>> cute, or
>> clever your code is. They just want a program that works.  I bet
>> Microsoft
>> can create cute some code, but it always doesn't work, does it?
>
> For my own readability purpose I didn't want to break up a series of IIF
> statements with
> an If Then ElseIf qualifier containing  a split assignment statement
> between them.
>
> -mhd
Author
11 Sep 2010 4:10 AM
Jason Keats
-mhd wrote:

> For my own readability purpose I didn't want to break up a series of IIF statements with
> an If Then ElseIf qualifier containing  a split assignment statement between them.

This problem was solved over 60 years ago...
http://en.wikipedia.org/wiki/Subroutine
Author
10 Sep 2010 7:53 PM
Henning
"-mhd" <not_r***@invalid.com> skrev i meddelandet
news:npnk86lm8balh9oon44vetbm3scv2fc9qa@4ax.com...
Well at least it's a challenge for me :-)

What is an elegant way of referring to the first string item delimited by a
space without
assigning to a string array variable as a separate step?

Normally you could do...
dim strArray() as string
MyStr = "1 2 3"
strArray = Split(MyStr)
Debug.Print strArray(0)

Debug.Print Split(MyStr,,1)   'return one sub string parameter naturally
doesn't work

Why am I doing this?
MyStr is already used in a complicated IIF statement but I only want the
first item in the
string.

While some may say that readability is an issue but many IIF statements in a
row are
actually the readability factor in this particular code (just the stuff in
the IIF may
require a second look).

Thanks!
-mhd

SomeString = Left$(MyStr, InStr(1, MyStr, " ") - 1)

/Henning