|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
One liner challengeWhat 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 -mhd wrote:
> Well at least it's a challenge for me :-) MyStr = "1 2 3"> > 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? Debug.Print Split(MyStr)(0) "Jim Mack" <no-uce-***@mdxi.com> wrote: Thanks! >MyStr = "1 2 3" >Debug.Print Split(MyStr)(0) -mhd In article <npnk86lm8balh9oon44vetbm3scv2fc***@4ax.com>,
not_r***@invalid.com says... Show quoteHide quote > MsgBox Split(mystrStr, " ")(0)> 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 David Kerber <dkerber@WarrenRogersAssociates.invalid> wrote:
>MsgBox Split(mystrStr, " ")(0) Thanks! -mhd -mhd wrote:
.... > What is an elegant way of referring to the first string item Debug.Print Mid$(MyStr,1,InStr$(MyStr," ")-1)> delimited by a space without assigning to a string array variable as > a separate step? .... > MyStr = "1 2 3" .... Not terribly elegant, but one line. .... > While some may say that readability is an issue but many IIF statements in a row are IIF() is, while compact, one of the most overhead intensive functions in > actually the readability factor in this particular code (just the stuff in the IIF may > require a second look). .... VB. I'd recommend avoiding it like the plague in lieu of almost any other construct. -- > IIF() is, while compact, one of the most overhead intensive functions in Why avoid it? If it's only called a couple of times, what difference does it > VB. I'd recommend avoiding it like the plague in lieu of almost any other > construct. 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. > > -- J.C. wrote:
>> IIF() is, while compact, one of the most overhead intensive functions OP says it's called often, first.>> 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. .... 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., ... -- dpb <n***@non.net> wrote:
>J.C. wrote: No I didn't.>>> 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. -mhd dpb <n***@non.net> wrote:
>Debug.Print Mid$(MyStr,1,InStr$(MyStr," ")-1) Thanks that works for me too!> >Not terribly elegant, but one line. -mhd dpb wrote:
<snip> > IIF() is, while compact, one of the most overhead intensive functions in Then why didn't you include the qualifier?> VB. I'd recommend avoiding it like the plague in lieu of almost any > other construct. > > -- 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. argusy wrote:
> dpb wrote: Becuz...rest of meaningless rant over nothing elided...> > <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? .... -- dpb wrote:
Show quoteHide quote > argusy wrote: Oh, thank you. Your reply says it all. No need to say any more>> 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... > > -- Graham argusy wrote:
Show quoteHide quote > dpb wrote: Previously answered.>> 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 IIF() sucks big time both performance-wise and legibility vis a vis alternatives. -- 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 youproblems 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/ 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 Well at least it's a challenge for me :-)news:npnk86lm8balh9oon44vetbm3scv2fc9qa@4ax.com... 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 "J.C." <y***@nothanks.com> wrote: For my own readability purpose I didn't want to break up a series of IIF statements with>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? an If Then ElseIf qualifier containing a split assignment statement between them. -mhd 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 For my own readability purpose I didn't want to break up a series of IIF 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? statements with an If Then ElseIf qualifier containing a split assignment statement between them. -mhd "Phil Hunt" <a**@aaa.com> wrote: True but in this case it is safe.>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 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 For my own readability purpose I didn't want to break up a series of IIF 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? statements with an If Then ElseIf qualifier containing a split assignment statement between them. -mhd On Fri, 10 Sep 2010 13:48:47 -0500, "J.C." <y***@nothanks.com> wrote: A while back I recall instructors stating that if you wrote the code>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. 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/ 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 -mhd wrote:
> For my own readability purpose I didn't want to break up a series of IIF statements with This problem was solved over 60 years ago...> an If Then ElseIf qualifier containing a split assignment statement between them. http://en.wikipedia.org/wiki/Subroutine "-mhd" <not_r***@invalid.com> skrev i meddelandet Well at least it's a challenge for me :-)news:npnk86lm8balh9oon44vetbm3scv2fc9qa@4ax.com... 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
Is including Excel9.olb in the installation files necessary?
E_Fail Status with VB6 Old vb6 / mdb app with "Could not delete from specified tables" er Aero Glass Control Text Problem Funky Font Enumeration Cant solve this. Error 217 Strange ActiveX-Exe behaviour Precalculated array How to test that a string can be represented by a font |
|||||||||||||||||||||||