; its funny, i never run a loop where not needed
; or call a function I know has loops inside
; a function that already has a loop, or worse
; calling this outside function within my loop.
; I think we used to call this coding, although
; that now means stitching together other
; people's functions i guess.
PROCEDURE StrNorm( VAR a:String )
VAR
b : String;
c,d : Byte;
BEGIN
b := '';
d := 0;
FOR c :=1 TO Length(a) IF (c=1) OR NOT (a[c-1]+a[c]=' ') THEN
BEGIN
IF NOT (a[c]=' ') THEN d := c
IF (d>0) THEN b := b + a[c];
END;
a := Left(b,d);
END;
Discussion on:
View:
Show:
I can't be bothered to go through the convoluted logic of your alternative function (which doesn't appear to be written in Visual Basic) but can it handle 3 spaces in a row??? Also, why the strage comment about not using loops (yet you do use a FOR loop in your version!!!)? Most software consists largely of loops and conditional branches or else it's very large and less versatile.
but if you cant translate what it would be in VB, well this thread is over your head.
K, we'll play a counting game
FUNCTION ConsumeResourses( a :String ):String
BEGIN
ConsumeResources := a + " You Suck!"
END
situ 1
BEGIN
Writeln( ConsumeResources('Demo'))
END
Situ 2
BEGIN
REPEAT
GotoXY(1,1);
Writeln( ConsumeResources('Demo'));
UNTIL (MemoryLeaksCauseCrashing=False);
END.
You can see how in Situ 2, the fuction is called many more times than in Situ 1, even though the output is essentially the same. Every time your OS calls a function, memory has to be set up, registers set up, RAM allocated, then when done, reverse all that. When you call a function from within a loop, or worse a function that calls a function in a loop, from a loop, it doesn't take long before well you've built a typcial piece of crap Windows app and finished your career without learning how to code.
In my first example posting, i didn't call any functions from within loops. In the second example, i didn't call any functions at all, except for Length(), which happened once. In the author's example, if that function was fed a string of say 200 characters with several spaces, how many times and functions would get called? Say it worked out to 10 "words". As i recall that should be 10x10x2 -- 200 calls to a sub-functions to complete the work. Mine has 1.
Its one thing to call a "built in" function, it gets worse when call one you made, that loops and function calls inself all over the place.
K, we'll play a counting game
FUNCTION ConsumeResourses( a :String ):String
BEGIN
ConsumeResources := a + " You Suck!"
END
situ 1
BEGIN
Writeln( ConsumeResources('Demo'))
END
Situ 2
BEGIN
REPEAT
GotoXY(1,1);
Writeln( ConsumeResources('Demo'));
UNTIL (MemoryLeaksCauseCrashing=False);
END.
You can see how in Situ 2, the fuction is called many more times than in Situ 1, even though the output is essentially the same. Every time your OS calls a function, memory has to be set up, registers set up, RAM allocated, then when done, reverse all that. When you call a function from within a loop, or worse a function that calls a function in a loop, from a loop, it doesn't take long before well you've built a typcial piece of crap Windows app and finished your career without learning how to code.
In my first example posting, i didn't call any functions from within loops. In the second example, i didn't call any functions at all, except for Length(), which happened once. In the author's example, if that function was fed a string of say 200 characters with several spaces, how many times and functions would get called? Say it worked out to 10 "words". As i recall that should be 10x10x2 -- 200 calls to a sub-functions to complete the work. Mine has 1.
Its one thing to call a "built in" function, it gets worse when call one you made, that loops and function calls inself all over the place.
I know I said I'd stop, but I just realized you, of all people, told someone else that this thread is over their head. This is some kind of elaborate joke, right?
PROCEDURE StrNorm( VAR a:String )
VAR
--b : Byte
--c : String[255];
BEGIN
--b := Length(a) ' counter
--c := ''; ' accumulator string
--WHILE (b>0) DO
--BEGIN
----IF (a=' ') THEN
----BEGIN
------WHILE (b>0) AND (a=' ') DO b := b - 1;
------IF (b>0) AND NOT (c='') THEN c := ' ' + c;
----END;
----IF (b>0) THEN
----BEGIN
------c := a + c;
------b := b - 1;
----END;
--END;
--a := c;
END;
VAR
--b : Byte
--c : String[255];
BEGIN
--b := Length(a) ' counter
--c := ''; ' accumulator string
--WHILE (b>0) DO
--BEGIN
----IF (a=' ') THEN
----BEGIN
------WHILE (b>0) AND (a=' ') DO b := b - 1;
------IF (b>0) AND NOT (c='') THEN c := ' ' + c;
----END;
----IF (b>0) THEN
----BEGIN
------c := a + c;
------b := b - 1;
----END;
--END;
--a := c;
END;
I've been coding for over twenty years, from Cobol to C# and VB.Net, including Forth, C++, PostScript, more versions of basic and assembly language than I can remember.
I'm sure I could make sense out of your code and messages if I were to make the effort, but I don't know why I would bother.
I'm sure I could make sense out of your code and messages if I were to make the effort, but I don't know why I would bother.
Well, I hate to be a kinds like people, because I don't know what that means. But it certainly is a lot more important than writing code that people can read without having to keep track of which variable is 'a' and which variable is 'b'. I've never used one character variables at all, and I don't know anyone else who has in about 15 years.
Actually, I don't wish you any luck at all, because that's just the way I roll. Why don't you go back to implementing Windows and Access in government and business and all aspects of same and leave writing code to people who have done so in this millenium.
Actually, I don't wish you any luck at all, because that's just the way I roll. Why don't you go back to implementing Windows and Access in government and business and all aspects of same and leave writing code to people who have done so in this millenium.
Hey, wait just a dag-nabbed minute. I just realized, I'm a coder.That's all I've done for 24 years, no management, no project lead, just because I like programming so much.
And mostly I code in c#, and the extension for c# files is .cs, so since I really am a coder and I code in c#, maybe I'm not a 'kinds like people' after all. Whadda ya think?
And mostly I code in c#, and the extension for c# files is .cs, so since I really am a coder and I code in c#, maybe I'm not a 'kinds like people' after all. Whadda ya think?
It's either Pascal, Fortran, HP SPL, or some other dead language, right? Do I get a prize? I could figure it out for sure, but first I want to know what the prize is?
This isn't VB on a VB site - and I shouldn't be told that I need to understand this code to be competent at VB. Also last I checked FOR was considered a loop.
I can't quite figure out what this code does using Pascal or C/C++. I don't understand what this statement does:
a[c-1]+a[c]=' '
Becuause these are BYTEs it should add the first byte to the second byte and have them equal the byte value of ' '. The only way that should work is if both bytes are 0 and the value of ' ' is 0 too. Using Ascii, ' ' = 00100000 (20 hex) so the 2 characters going in would have to be non-printable characters.
To correct this you should need to do an AND so the IF statement should read:
IF (c=1) OR NOT ((a[c-1]=' ') AND (a[c]=' ')) THEN ...
It still doesn't read as easily as the original code. I also think it allows the first letter be a space or else it ends up removing ALL spaces.
I have always been taught that it is generally best to use the built in functions whenever possible because they should already be optimized.
I can't quite figure out what this code does using Pascal or C/C++. I don't understand what this statement does:
a[c-1]+a[c]=' '
Becuause these are BYTEs it should add the first byte to the second byte and have them equal the byte value of ' '. The only way that should work is if both bytes are 0 and the value of ' ' is 0 too. Using Ascii, ' ' = 00100000 (20 hex) so the 2 characters going in would have to be non-printable characters.
To correct this you should need to do an AND so the IF statement should read:
IF (c=1) OR NOT ((a[c-1]=' ') AND (a[c]=' ')) THEN ...
It still doesn't read as easily as the original code. I also think it allows the first letter be a space or else it ends up removing ALL spaces.
I have always been taught that it is generally best to use the built in functions whenever possible because they should already be optimized.
I'll try to be polite. Maybe he ended up in a VB6 discussion because he has some custom COM that does inline Pascal, like using inline assembly in basic in the old DOS days.
As far as his string constant, he should look into Pascal's Wager and take him up on it.
While memory manipulation may be faster in Pascal and maybe in C, I think most programmers woud agree that using string manipulation is a much safer way to code, particularly with built in string functions.
Lastly, complex loops should always have a means of escape (i.e. a error trap), but since this loop acts upon a string variable of some finite length, there is no need. P.S. to another post, that is also why there is no need for a DoEvents statement.
As far as his string constant, he should look into Pascal's Wager and take him up on it.
While memory manipulation may be faster in Pascal and maybe in C, I think most programmers woud agree that using string manipulation is a much safer way to code, particularly with built in string functions.
Lastly, complex loops should always have a means of escape (i.e. a error trap), but since this loop acts upon a string variable of some finite length, there is no need. P.S. to another post, that is also why there is no need for a DoEvents statement.
He's stuck in the 80's when you had to worry about such nonsense to make up for limited processing power. I remember coding 6502 and Z80 processors in assembly and playing tricks to save cycles. Haven't thought about that in years.
He's not aware that in software production, as in every industry, as processing power increases, part of it is used to provide a more flexible production (programming) environment. My guess is that he hasn't coded since before multitasking hit the desktop big-time with Windows 3.0.
Probably also not aware of the development of optimizing compilers.
He's not aware that in software production, as in every industry, as processing power increases, part of it is used to provide a more flexible production (programming) environment. My guess is that he hasn't coded since before multitasking hit the desktop big-time with Windows 3.0.
Probably also not aware of the development of optimizing compilers.
is a double space, but the input box or posting function on this web site, took out the 2nd space.
Dude, I was gonna dog you until you drop, but I see there's a failure of intelligence or communication somewhere, so I'm gonna let it go.
How about a recursive version ?
Procedure NormaliseString(Var Str : String);
Begin
While Pos(' ',Str) > 0 Do
Str := NormaliseString(Delete(Str,Pos(' ',Str),1));
End;
Tee Hee
Come on you all did the Towers of Hanoi example you can figure it out, even if you've never used it since.
Procedure NormaliseString(Var Str : String);
Begin
While Pos(' ',Str) > 0 Do
Str := NormaliseString(Delete(Str,Pos(' ',Str),1));
End;
Tee Hee
Come on you all did the Towers of Hanoi example you can figure it out, even if you've never used it since.
So, it took you 12 (and later 19) lines of code (once you remove the Function/Procedure definition statements) to accomplish(?) the same thing that Peter did in 5. 2.4 to 3.8 times as large. Brilliant.
After you thought about it for a while (billing your client for your time I suppose), in your second example ("2nd thoughts") you modified your flawed routine to walk the string backwards to correct its inability to handle strings containing leading or 3 or more consecutive spaces, but in the process you included 2 WHILE loops, the very thing you ragged at Peter about. Also, both of your solutions require defining an output string ("b") while Peter's modifies the original in place. Strange for someone so "storage conscious".
An earlier TechRepublic article this week pointed out that upwards of 70% of IT's budget can be spent on maintenance. I would MUCH rather work with Peter's code than with your indecipherable crap.
You volunteered that you "haven't coded much in years". Whatever it is you've been doing since you stopped coding, maybe you should consider going back to it because the Professional programming world has left you behind. And as one of your coding examples appropriately pointed out: You Suck !
After you thought about it for a while (billing your client for your time I suppose), in your second example ("2nd thoughts") you modified your flawed routine to walk the string backwards to correct its inability to handle strings containing leading or 3 or more consecutive spaces, but in the process you included 2 WHILE loops, the very thing you ragged at Peter about. Also, both of your solutions require defining an output string ("b") while Peter's modifies the original in place. Strange for someone so "storage conscious".
An earlier TechRepublic article this week pointed out that upwards of 70% of IT's budget can be spent on maintenance. I would MUCH rather work with Peter's code than with your indecipherable crap.
You volunteered that you "haven't coded much in years". Whatever it is you've been doing since you stopped coding, maybe you should consider going back to it because the Professional programming world has left you behind. And as one of your coding examples appropriately pointed out: You Suck !
In Pascal, this (or the variation posted later) would probably work moderately well. However, in VB, this type of string manipulation isn't quite as efficient, since VB allocates memory for strings very badly. Even using the Mid statement (not the function, the statement), something most VB programmers are less than fully familiar with, you will likely be allocating memory for string comparisons and the like, wasting FAR more time than you would actually save.
Your approach, while better in C/Pascal/Assembly, though it could be improved by manipulating the input string directly, would probably be less workable and less efficient in VB.
If you REALLY want to prove your point to everybody who has flamed you in this thread, write the procedure in VB, and give us time comparisons of a looped replace vs. your method.
Rob
Your approach, while better in C/Pascal/Assembly, though it could be improved by manipulating the input string directly, would probably be less workable and less efficient in VB.
If you REALLY want to prove your point to everybody who has flamed you in this thread, write the procedure in VB, and give us time comparisons of a looped replace vs. your method.
Rob
Just FYI, I tried programming three different methods and timed each of them.
Method 1: Replace 2 spaces by 1 until no more double-spaces.
Method 2: Scan string and move each character downwards except if there are multiple spaces.
Method 3: Replace 2^x+1 spaces with 1 space, decreasing x until you're done.
Results varied widely between the three methods, each having cases where it did better or worse than the others.
Method 1 did best overall, with its strength being for long strings with short lengths of white space in them.
Method 2 did best on shorter strings.
Method 3 vastly outperformed the others on longer strings that were nearly all spaces.
Rob
Method 1: Replace 2 spaces by 1 until no more double-spaces.
Method 2: Scan string and move each character downwards except if there are multiple spaces.
Method 3: Replace 2^x+1 spaces with 1 space, decreasing x until you're done.
Results varied widely between the three methods, each having cases where it did better or worse than the others.
Method 1 did best overall, with its strength being for long strings with short lengths of white space in them.
Method 2 did best on shorter strings.
Method 3 vastly outperformed the others on longer strings that were nearly all spaces.
Rob
Three points:
1) I always include a DoEvents command inside a loop unless speed is of vital importance.
2) Why use String(2, " ") when " " is significantly shorter?
3) I find tabs instead of spaces much more of a pain as they are 'invisible' and may be mistaken for multiple spaces. To change the function so that it replaces tabs with spaces, replace both occurrences of String(2, " ") with Chr$(9).
1) I always include a DoEvents command inside a loop unless speed is of vital importance.
2) Why use String(2, " ") when " " is significantly shorter?
3) I find tabs instead of spaces much more of a pain as they are 'invisible' and may be mistaken for multiple spaces. To change the function so that it replaces tabs with spaces, replace both occurrences of String(2, " ") with Chr$(9).
Rather than replace the double space check, add it to the function outside the loop.
The reason for the String(2," ") is because the " " becomes a string constant that has to be looked up in the programs data page, while String is a function that returns a temporary string value. Unless dealing with a rather large string variable, either should perform about the same. The finite size of the string variable is also the reason that a DoEvents command is not really needed.
The reason for the String(2," ") is because the " " becomes a string constant that has to be looked up in the programs data page, while String is a function that returns a temporary string value. Unless dealing with a rather large string variable, either should perform about the same. The finite size of the string variable is also the reason that a DoEvents command is not really needed.
The author probably used String(2," ") because code with double spaces might not display nicely in a browser as strait text.
An even more useful function might be
0
s = Replace(s, DupLiteral, DupTypeToRemove)
Loop
RemoveDupChars = s
End Function
An even more useful function might be
0
s = Replace(s, DupLiteral, DupTypeToRemove)
Loop
RemoveDupChars = s
End Function
Why not use the Trim() key word, then every body knows what going on, i.e . removes leading and trailing spaces, and those unwanted double spaces as well.
Paul
Paul
I actually tested your claim because it was so outrageous I had to make sure I wasn't tripping. That was 10 wasted minutes I'll never get back.
Probably a lot less embarassing for you if you tested before posting as well.
Here's what MS says it does.
... Visual Basic 6.0 > Reference >
Visual Basic for Applications Reference
LTrim, RTrim, and Trim Functions
...
Returns a Variant (String) containing a copy of a specified string without leading spaces (LTrim), trailing spaces (RTrim), or both leading and trailing spaces (Trim).
Probably a lot less embarassing for you if you tested before posting as well.
Here's what MS says it does.
... Visual Basic 6.0 > Reference >
Visual Basic for Applications Reference
LTrim, RTrim, and Trim Functions
...
Returns a Variant (String) containing a copy of a specified string without leading spaces (LTrim), trailing spaces (RTrim), or both leading and trailing spaces (Trim).
I also needed all of my embedded single qoutes to be escaped in my sql statement, so I just run it through Trim as well, and viola!
Trim("Select name from Emp where skill = 'cscoder's gullible'")
returns
Select name from Emp where skill = 'cscoder's gullible'
Trim("Select name from Emp where skill = 'cscoder's gullible'")
returns
Select name from Emp where skill = 'cscoder's gullible'
I'm not interested enough to figure out what you're trying to say, but I'm afraid that sinqle quote in 'cscoder's gullible' is going to give you problems.
Are you sure you're not @TechRepublic in disquise, there are marked similarities.
Are you sure you're not @TechRepublic in disquise, there are marked similarities.
I guess I am gullible, because I can't figure out what your example means. Are you trying to say the spaces don't need to be normalized in SQL? I think we all know that, except for TechRepublic@.
Otherwise I'm really missing your point. All of this is pretty much fit for a beginning VB class, I just couldn't resist blasting someone who posts crappy pascal and then tells someone else that the thread is over their head because they won't go to the trouble of deciphering his crappy code.
How did I gore your ox?
Otherwise I'm really missing your point. All of this is pretty much fit for a beginning VB class, I just couldn't resist blasting someone who posts crappy pascal and then tells someone else that the thread is over their head because they won't go to the trouble of deciphering his crappy code.
How did I gore your ox?
I misread your message because we were talking about getting rid of double-spaces and you're talking about escaping single-quotes (?). I guess your code was a joke because nothing changed.
If you're flaming me for testing his assertion, you can bite me. Up until .NET, I primarily coded in C++. Now that trim is available in both languages, I use it all the time, but I doubt I ever checked to see that it left embedded spaces alone. Since I was gonna flame a guy, I made sure I knew what I was talking about, despite the documentation.
If you seriously think that line of SQL is gonna work, then I don't have to test a damn thing to pronounce you full of s**t.
You either need help with your coding or your comedy act.
If you're flaming me for testing his assertion, you can bite me. Up until .NET, I primarily coded in C++. Now that trim is available in both languages, I use it all the time, but I doubt I ever checked to see that it left embedded spaces alone. Since I was gonna flame a guy, I made sure I knew what I was talking about, despite the documentation.
If you seriously think that line of SQL is gonna work, then I don't have to test a damn thing to pronounce you full of s**t.
You either need help with your coding or your comedy act.
by letting this thing end. Your other post is just hanging there so big and juicy. If you don't reply, I'll back off, otherwise I'll hang on a comment you won't like. So you better be ready to bring it.
Maybe you're young or something, because this is too much like shooting fish in a barrel. I don't want to beat up a kid even if he is obnoxious.
I'm actually gonna stop trying to respond when other people get flamed unjustly, because it just opens up a Pandora's box of people who don't care about fairness and just want to try and embarass anybody they can. I try to just go after people who deserve it, like you.
Do you actually know what 'gullible' means?
http://dictionary.reference.com/search?q=gullible
I'm kinda sorry you had to take the heat on this. I like to get along, but when I'm attacked there is no length I won't go to verbally, in an Oscar Wilde kind of way, to crush my attacker.
Maybe you're young or something, because this is too much like shooting fish in a barrel. I don't want to beat up a kid even if he is obnoxious.
I'm actually gonna stop trying to respond when other people get flamed unjustly, because it just opens up a Pandora's box of people who don't care about fairness and just want to try and embarass anybody they can. I try to just go after people who deserve it, like you.
Do you actually know what 'gullible' means?
http://dictionary.reference.com/search?q=gullible
I'm kinda sorry you had to take the heat on this. I like to get along, but when I'm attacked there is no length I won't go to verbally, in an Oscar Wilde kind of way, to crush my attacker.
I have been referencing the regular expression library in my projects for years...you could have a regular expression do all that work without a loop at all...
the regular expression might have a loop in it somewhere.
Only a guess but I feel it's unlikely that VB is optimising the while statement to it best possible potential, simply because you could do that as double space could be held as a word or an integer depending on what size of character was being processed. I can't think of any reason at all why any compiler would go to this length for optimising searching on 1 -2 or 1-4 character strings. Equally, except in extremely extreme circumstances I fail to see the value of optimising in terms of the effort required vs the reward gained. Two days work for thirty clock cycles just doesn't fire me up and might get me fired.
Only a guess but I feel it's unlikely that VB is optimising the while statement to it best possible potential, simply because you could do that as double space could be held as a word or an integer depending on what size of character was being processed. I can't think of any reason at all why any compiler would go to this length for optimising searching on 1 -2 or 1-4 character strings. Equally, except in extremely extreme circumstances I fail to see the value of optimising in terms of the effort required vs the reward gained. Two days work for thirty clock cycles just doesn't fire me up and might get me fired.
Exactly.
I gotta get away from this thread, but TechRepublic@ was just too juicy of a target, and now I've got slackola as well.
I gotta get away from this thread, but TechRepublic@ was just too juicy of a target, and now I've got slackola as well.
If you're pulling all of the spaces out;
NewString = Join(Split(OldString, " "), "")
You could do this to replace multiple spaces with single spaces, too, but it would then replace 3 or four spaces with two; so you would need to put it into a while loop.
NewString = Join(Split(OldString, " "), "")
You could do this to replace multiple spaces with single spaces, too, but it would then replace 3 or four spaces with two; so you would need to put it into a while loop.
Fuggeddaboudit . . . I misread the article =]
I read it as removing all spaces . . .
I read it as removing all spaces . . .
- Keyboard Shortcuts:
- Prev
- Next
- Toggle

































