Comment On Just in case SPACE isn't supported in the future

Jake here, in classic programmer fashion, late on the first update.  Anyhoo, we're revisiting some classic WTFs this week while Alex is on vacation or undergoing surgery or whatever he said he was doing.  So here's one of my personal favorite WTFs from Scott Elkin for you to read while wishing Alex a happy vacation or speedy recovery. [expand full text]
« PrevPage 1 | Page 2Next »

Re: Just in case SPACE isn't supported in the future

2005-08-08 21:49 • by clambaker
nice

Re: Just in case SPACE isn't supported in the future

2005-08-08 22:00 • by paranoidgeek
In PHP :



$text = preg_replace("/[!@#$%^&\*\(\)\-_\+=]/", '', $text);



Hmm maybe the other way is simpler ?



Re: Just in case SPACE isn't supported in the future

2005-08-08 22:24 • by Lenny
40258 in reply to 40256
Anonymous:
nice




Wow, insightful and humorus!  Thank you for sharing :)

Re: Just in case SPACE isn't supported in the future

2005-08-08 22:47 • by OmnipotentEntity
40259 in reply to 40257
yes, Perl:



$text =~ s/[!@\#\$%\^&*\(\)-_+=]//;

Re: Just in case SPACE isn't supported in the future

2005-08-08 22:56 • by SerajewelKS
While this is ugly, it's faster than looping through an array. So
either he's dumb, or this code runs so often that it warrants a little
optimization.  (Heh... optimization of VB/VBScript...)

Re: Just in case SPACE isn't supported in the future

2005-08-08 22:58 • by SerajewelKS
40262 in reply to 40259
Anonymous:
yes, Perl:



$text =~ s/[!@\#\$%\^&*\(\)-_+=]//;


Or...



$text =~ s/\W//g;

Re: Just in case SPACE isn't supported in the future

2005-08-08 23:56 • by OmnipotentEntity
40263 in reply to 40262
umm... I wrote it to spec (I don't know if he wants to keep "s and :s
and |s and such).  And that removes spaces too, though I admit to
forgetting the global...  What you're looking for is:



$text =~ s/[\W\S]//g;



or perhaps



$text =~ s/[?:\W\S]//g;



if you have something in $1 that you want to keep.

Re: Just in case SPACE isn't supported in the future

2005-08-08 23:58 • by OmnipotentEntity
40264 in reply to 40263
Oh yeah, and he wants to get rid of _s too, so



$text =~ s/[?:\W\S_]//g;

Re: Just in case SPACE isn't supported in the future

2005-08-09 00:13 • by OmnipotentEntity
40265 in reply to 40264
... actually that just deletes every character in the string, or throw
a syntax error because I'm confusing () with [] again.  Perhaps I
should start thinking about this shit before I post. ¬_¬



What I meant to say is:



$text =~ s/[^a-zA-Z0-9\s]//g;

Re: Just in case SPACE isn't supported in the future

2005-08-09 00:17 • by OmnipotentEntity
40266 in reply to 40265
But all this is moot, because AFAIK VB only supports regular
expressions in a convoluted, complicated way.  And I promise to
stop posting now.

Re: Just in case SPACE isn't supported in the future

2005-08-09 00:25 • by Ayende Rahien
40268 in reply to 40260
SerajewelKS:
While this is ugly, it's faster than looping through an array. So
either he's dumb, or this code runs so often that it warrants a little
optimization.  (Heh... optimization of VB/VBScript...)




It's /creatainly/ not faster.

The replace is going to iterate the string per each symbol! Over & over again.

Re: Just in case SPACE isn't supported in the future

2005-08-09 00:27 • by AC
40269 in reply to 40260
what the fsck do you think the Replace() function does? VB compiles to
native. 10+ function calls and 10+ variant allocs is faster than
looping through an array? WTF? god explain that please

Re: Just in case SPACE isn't supported in the future

2005-08-09 00:30 • by AC
40270 in reply to 40260
SerajewelKS:
While this is ugly, it's faster than looping through an array. So
either he's dumb, or this code runs so often that it warrants a little
optimization.  (Heh... optimization of VB/VBScript...)




argh i meant to quote this weenie...



Replace() will of course have to loop through something... argh

Re: Just in case SPACE isn't supported in the future

2005-08-09 00:47 • by luis
40271 in reply to 40268
Ayende Rahien:
It's /creatainly/ not faster. The replace is going to iterate the string per each symbol! Over & over again.


I hope you don't mean to imply it's slower. Let n be the length of the string, and m the number of distinct symbols. Then in one case you're looping m times over the list of symbols, and in each iteration, you do an inner loop over the length of the string (n), where you check if the current char is the current symbol, and replace it if it is. In the other case, you loop over each character of the string, and then for each symbol, you check if the current char is that symbol. In both cases, it's n*m. The real inefficiency probably is that the Replace() function used needs to do a lot of bookkeeping, either by allocating a new string for the result, or by shifting characters towards the beginning of the string when you delete one. (The fast thing to do is to allocate a new, mutable string, iterate over the source string, and for each character that's not being removed, append it to the new string...)

Re: Just in case SPACE isn't supported in the future

2005-08-09 01:11 • by Drak
40272 in reply to 40271

Anonymous:
(The fast thing to do is to allocate a new, mutable string, iterate over the source string, and for each character that's not being removed, append it to the new string...)


 


And how is adding characters to a string in VB fast? It's almost the slowest operation possible [:P]. Maybe an array would do the job, 'join'ing it at the end. Anywho this person should be shot, because well.. do you know how many 'symbols' there are? He'd have been better off just copying letters to the target array or string.


Drak

Re: Just in case SPACE isn't supported in the future

2005-08-09 01:25 • by Omnifarious
40274 in reply to 40260
SerajewelKS:
While this is ugly, it's faster than looping through an array. So
either he's dumb, or this code runs so often that it warrants a little
optimization.  (Heh... optimization of VB/VBScript...)




Well, that would be interesting test.  Internally VB is going to
implement each one of those Replace calls with a loop (though probably
implemented in nice, fast C/C++ code) through the array.  So all
those nested Replace calls will eventually (as the level of nesting
grows deeper and deeper) be slower than some code that can loop through
the array just once.

Re: Just in case SPACE isn't supported in the future

2005-08-09 01:31 • by Michael Casadevall
40276 in reply to 40274
It's burn in a way that is worse then table streching. I'm afraid to see the other half of the replacement query.

Re: Just in case SPACE isn't supported in the future

2005-08-09 02:52 • by William

nice...


another WTF is that I cannot browse this post using FireFox 1.0.6 [*-)] I need to switch to IE in viewing it....

Re: Just in case SPACE isn't supported in the future

2005-08-09 03:01 • by mdecarle
40281 in reply to 40274

Omnifarious:
Well, that would be interesting test.  Internally VB is going to implement each one of those Replace calls with a loop (though probably implemented in nice, fast C/C++ code) through the array.  So all those nested Replace calls will eventually (as the level of nesting grows deeper and deeper) be slower than some code that can loop through the array just once.
I don't think the VB compiler will translate to C/C++ before it compiles it ...


I've done regular expressions in VB6 and VBScript. 'Regex' is part of the "Scripting Runtime", and is used to interprete VBScript. It also has such interesting things as objects for files and folders (via the FileSystemObject). Most modern VB6 projects will have this DLL referenced anyway. So it is only a question of creating a object, and passing along the expression.

Re: Just in case SPACE isn't supported in the future

2005-08-09 03:10 • by MWTJ
Are VB strings mutable or immutable?  If they're immutable, then
this is allocating and GCing 14 strings every time it happens. 
It's
certainly a WTF, more like, "dude, learn how to use the library" kind
of WTF. 
It's certainly not an OMGWTF, like the false detector was.  Still,
it's great to see the lengths people will go to to avoid having to use
convenient built-in language features like regexps.  Scary to
think what this programmer would do in Java.  "I will abstain from
using 94% of java.* packages."



----------------------

mobile search

Re: Just in case SPACE isn't supported in the future

2005-08-09 03:12 • by ammoQ
Without profiling, I would not be too sure this is slower than alternative implementations.

Depending on how you build it (RE would of course be the best way, but
not every programming language supports them) your own implementation
may be slower.

Let's say
text="a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!"
and lets assume the replace function is clever enough not allocate a
new string if no replacement is actually made.

One alternative implementation would loop through the string, check
each character and remove it if it is a symbol: text=left(text,
i-1)&mid(text,i+1)

This creates 3 new string objects for each "!"

Alternatively, you build a new string with all the characters which are not symbols:

newtext=newtext&mid(text,i,1)

This creates 2 new string objects for each "a"

Chances are that both versions are slower than the original "WTF".

Anyway, chances are you will not notice the difference in real life.

At least you can say for sure that this implementation works. No doubt.

The alternatives with loops require more testing, because they are more complex.

Re: Just in case SPACE isn't supported in the future

2005-08-09 03:36 • by Masklinn
40285 in reply to 40279
Anonymous:

nice...


another WTF is that I cannot browse this post using FireFox 1.0.6 [*-)] I need to switch to IE in viewing it....


Problem's yours, I browse TDWTF just fine with Firefox 1.0.6


Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.10) Gecko/20050716 Moonape/1.0.6

Re: Just in case SPACE isn't supported in the future

2005-08-09 04:38 • by Bad Wolf
40286 in reply to 40279
Anonymous:

nice...


another WTF is that I cannot browse this post using FireFox 1.0.6 [*-)] I need to switch to IE in viewing it....





Hit refresh - worked for me.  Though the text then decided it was
about 3 screens wide, so I had to do a lot of scrollbar work..

Re: Just in case SPACE isn't supported in the future

2005-08-09 05:33 • by DZ-Jay
40287 in reply to 40265
OmnipotentEntity:
... actually that just deletes every character in the string, or throw
a syntax error because I'm confusing () with [] again.  Perhaps I
should start thinking about this shit before I post. ¬_¬



What I meant to say is:



$text =~ s/[^a-zA-Z0-9\s]//g;




But that's mostly the same as \W, except that Perl takes the underscore as a "word character", so:

 $text =~ s/[\W_]//go;



    -dZ.



Re: Just in case SPACE isn't supported in the future

2005-08-09 05:35 • by DZ-Jay
40288 in reply to 40266
OmnipotentEntity:
But all this is moot, because AFAIK VB only supports regular
expressions in a convoluted, complicated way.  And I promise to
stop posting now.




Actually, it supports it as an external Class, which is the way many
other non-Perl languages do it.  As far as I know, Perl is one of
the few (the only) programming languages that integrate Regular
Expressions into their core syntax.  (Just like god intended
RegExps to be.)



    -dZ.

Re: Just in case SPACE isn't supported in the future

2005-08-09 05:48 • by dhromed
40289 in reply to 40288
DZ-Jay:
As far as I know, Perl is one of
the few (the only) programming languages that integrate Regular
Expressions into their core syntax.  (Just like god intended
RegExps to be.)



    -dZ.




I thought, correct me if I'm wrong, that Perl does not so much support
regular expressions as much as it is a programming language that
evolved from regular expressions. Which is arguably why so many
C/ECMA/Java coders think a slab of Perl is alien-sp33k. It's like
extensive regex with keywords and function names strewn in, instead of
textual code with a regex here and there. :)

Re: Just in case SPACE isn't supported in the future

2005-08-09 06:09 • by AndrewVos (argh im not logged in again)
40290 in reply to 40289

mmm, in Vb.Net it woulda looked soo much cooler :>


 = .Replace("%","").Replace("^") (repeat 256 times)


 

Re: Just in case SPACE isn't supported in the future

2005-08-09 06:09 • by diGriz
*lol* Just great. YMMD!

Re: Just in case SPACE isn't supported in the future

2005-08-09 07:04 • by SteveM
40293 in reply to 40290
Anonymous:

mmm, in Vb.Net it woulda looked soo much cooler :>


 = .Replace("%","").Replace("^") (repeat 256 times)






Er, in Vb.net it wouldn't look too bad at all actually -





strOut = Regex.Replace(strIn, "[^\w\.@-]", "")



(BTW ignore the RegEx I quote, but that's the syntax)

Re: Just in case SPACE isn't supported in the future

2005-08-09 07:14 • by Richy
40294 in reply to 40257

paranoidgeek:
In PHP :

$text = preg_replace("/[!@#$%^&\*\(\)\-_\+=]/", '', $text);

Hmm maybe the other way is simpler ?


 


$text = preg_replace('#[\W_]#', '', $text);


Much nicer...

Re: Just in case SPACE isn't supported in the future

2005-08-09 08:25 • by DZ-Jay
40295 in reply to 40289
dhromed:


I thought, correct me if I'm wrong, that Perl does not so much support
regular expressions as much as it is a programming language that
evolved from regular expressions.





You're wrong.  Perl was designed from the very beginning as a
programming language, with the built-in feature of manipulating
text.  It borrowed heavily from text-manipulation tools (awk, for
example) for it syntax and core functions, since those tools were very
powerful at what they did, and their syntax and mode of operation was
well known and understood.  Because of this, syntax for performing
operations on Regular Expressions is built into the language itself, as
opposed to having to call an external function or instantiate an
object, like other programming languages require.  Of course, this
will look alien to anybody not familiar with its syntax -- just like
Japanese and Korean manuscripts look very alien to those who do not
understand it (like me!).  This does not mean that it is
inherently more complex or harder to learn than any other language --
after all, little Japanese kids read and write their language very
naturally at an early age, just like Americans do with English.



    dZ.



Re: Just in case SPACE isn't supported in the future

2005-08-09 08:52 • by Jason
Some of these responses are a WTF. The regex /[!@#$%^&*()_+=-]//g would suffice to do the replacement, assuming the "programmer" intended to ignore other puncuation. No need to backslash escape the special characters in the regex, just put the hyphen at the end. And it would run in O(n), not O(m*n), because the regex uses a table lookup, not a comparison against each character. Though there would be some overhead from compiling the regex.

Re: Just in case SPACE isn't supported in the future

2005-08-09 08:56 • by MORB
40297 in reply to 40286
Anonymous:



Hit refresh - worked for me.  Though the text then decided it was
about 3 screens wide, so I had to do a lot of scrollbar work..




That's because the code is all on one line, so it's
actually a WTF from the original code.

Re: Just in case SPACE isn't supported in the future

2005-08-09 09:05 • by tufty
40298 in reply to 40295
> just like Americans do with English.

Heh. You misspelt "American"

Simon

You're right about perl, though :D

Re: Just in case SPACE isn't supported in the future

2005-08-09 09:14 • by cm5400
40299 in reply to 40282

MWTJ:
Are VB strings mutable or immutable?  If they're immutable, then this is allocating and GCing 14 strings every time it happens.  It's certainly a WTF, more like, "dude, learn how to use the library" kind of WTF.  It's certainly not an OMGWTF, like the false detector was.  Still, it's great to see the lengths people will go to to avoid having to use convenient built-in language features like regexps.  Scary to think what this programmer would do in Java.  "I will abstain from using 94% of java.* packages."


VB Strings are immutable.  It will create a new string for every change to it.

Re: Just in case SPACE isn't supported in the future

2005-08-09 09:56 • by travisowens
40300 in reply to 40260
SerajewelKS:
... or this code runs so often that it warrants a little optimization.  (Heh... optimization of VB/VBScript...)
Well if it is VB/VBScript then you need all the potimizing you can get!

Re: Just in case SPACE isn't supported in the future

2005-08-09 09:56 • by Paul
40301 in reply to 40265
OmnipotentEntity:
... actually that just deletes every character in the string, or throw
a syntax error because I'm confusing () with [] again.  Perhaps I
should start thinking about this shit before I post. ¬_¬



What I meant to say is:



$text =~ s/[^a-zA-Z0-9\s]//g;




This is, arguably, the funniest fibre in this thread. Thank you for the laugh and the mini WTF.

Re: Just in case SPACE isn't supported in the future

2005-08-09 10:01 • by travisowens
40302 in reply to 40279
Anonymous:
another WTF is that I cannot browse this post using FireFox 1.0.6 [*-)] I need to switch to IE in viewing it....
The whole DailyWTF site is a WTF, we can't edit our posts and setting fonts attributes in the post editor is flakely.

Re: Just in case SPACE isn't supported in the future

2005-08-09 10:08 • by phred

Where I work we have to use that stuff, as regular expressions are forbidden, we aren't allowed to install any regular expression libraries as the GPL will corrupt our proprietory applications and open us up to lawsuits.


But its not too bad as the code tends to be self documenting, unlike regular expressions which frequently have shell code embedded which can allow breakins to occur, this happens with perl too.

Re: Just in case SPACE isn't supported in the future

2005-08-09 10:28 • by dubwai
40304 in reply to 40283

ammoQ:
Without profiling, I would not be too sure this is slower than alternative implementations.
Depending on how you build it (RE would of course be the best way, but not every programming language supports them) your own implementation may be slower.
Let's say text="a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!" and lets assume the replace function is clever enough not allocate a new string if no replacement is actually made.
One alternative implementation would loop through the string, check each character and remove it if it is a symbol: text=left(text, i-1)&mid(text,i+1)
This creates 3 new string objects for each "!"
Alternatively, you build a new string with all the characters which are not symbols:
newtext=newtext&mid(text,i,1)
This creates 2 new string objects for each "a"
Chances are that both versions are slower than the original "WTF".
Anyway, chances are you will not notice the difference in real life.
At least you can say for sure that this implementation works. No doubt.
The alternatives with loops require more testing, because they are more complex.


Why would one do any of these?  Is there no mutable String type in VB or something? Can you not create Strings from arrays of characters?

Re: Just in case SPACE isn't supported in the future

2005-08-09 10:39 • by phred
40305 in reply to 40304
dubwai:

ammoQ:
Without profiling, I would not be too sure this is slower than alternative implementations.
Depending on how you build it (RE would of course be the best way, but not every programming language supports them) your own implementation may be slower.
Let's say text="a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!a!" and lets assume the replace function is clever enough not allocate a new string if no replacement is actually made.
One alternative implementation would loop through the string, check each character and remove it if it is a symbol: text=left(text, i-1)&mid(text,i+1)
This creates 3 new string objects for each "!"
Alternatively, you build a new string with all the characters which are not symbols:
newtext=newtext&mid(text,i,1)
This creates 2 new string objects for each "a"
Chances are that both versions are slower than the original "WTF".
Anyway, chances are you will not notice the difference in real life.
At least you can say for sure that this implementation works. No doubt.
The alternatives with loops require more testing, because they are more complex.


Why would one do any of these?  Is there no mutable String type in VB or something? Can you not create Strings from arrays of characters?



toreplace = "!*/\&^%$#@"


for i = 1 to len(toreplace)


  text = replace(text,mid$(toreplace,i,1),"")


next i


I'd do something like that, although it doesn't save on the string copying.

Re: Just in case SPACE isn't supported in the future

2005-08-09 10:44 • by Mung Kee
You know it's a WTF when you can only see half of it in your browser window.

Re: Just in case SPACE isn't supported in the future

2005-08-09 10:55 • by Zathrus
40307 in reply to 40303
Anonymous:

Where I work we have to use that stuff, as
regular expressions are forbidden, we aren't allowed to
install any regular expression libraries as the
GPL will corrupt our proprietory applications and open us up to
lawsuits.




I'm sorry you work in such an utterly clue-free workplace. There are
a number of non-GPL licensed RegEx libraries for most languages (those
that don't just have them built-in that is).



Anonymous:


But its not too bad as the code tends to be self documenting, unlike
regular expressions which frequently have shell code embedded which can
allow breakins to occur, this happens with perl too.





Oh, nevermind. You deserve to work there.

Re: Just in case SPACE isn't supported in the future

2005-08-09 11:07 • by joodie
40308 in reply to 40307
Anonymous:
Anonymous:

Where I work we have to use that stuff, as
regular expressions are forbidden, we aren't allowed to
install any regular expression libraries as the
GPL will corrupt our proprietory applications and open us up to
lawsuits.




I'm sorry you work in such an utterly clue-free workplace. There are
a number of non-GPL licensed RegEx libraries for most languages (those
that don't just have them built-in that is).



Anonymous:


But its not too bad as the code tends to be self documenting, unlike
regular expressions which frequently have shell code embedded which can
allow breakins to occur, this happens with perl too.





Oh, nevermind. You deserve to work there.




Just ignore the troll.







Re: Just in case SPACE isn't supported in the future

2005-08-09 11:19 • by JThelen
40309 in reply to 40302
travisowens:
Anonymous:
another WTF is that I
cannot browse this post using FireFox 1.0.6 [*-)] I need to switch to
IE in viewing it....
The whole DailyWTF site is a WTF, we can't
edit our posts and setting fonts attributes in the post editor is
flakely.




IMO, not being able to edit your posts is no big loss;  it just
forces you to proofread what you're posting.  I go to enough
no-edit forums that I don't miss it.



As to the 'whole site' being one big WTF, the only real issue is the
javascript page browsing.  Everything else is sporadic enough that
it qualifies as a PEBKAC error, not a problem with the site itself,
unless you want to get into how the site displays in Opera or
Safari.  That's just a standard MS WTF.

Re: Just in case SPACE isn't supported in the future

2005-08-09 11:39 • by Mung Kee
40310 in reply to 40309
JThelen:
As to the 'whole site' being one big WTF, the only real issue is the
javascript page browsing.




I agree.  Lose the form post in the paginator.  You can't refresh without choosing to repost the data.

Re: Just in case SPACE isn't supported in the future

2005-08-09 12:44 • by Manni

I hate to burst everyone's bubble (actually I love to do that), but this is one of the best ways to do it in VB. I wrote a little test app that did the RegExp solution and it averaged 0.5 - 0.6 ms per execution. The replace version above ran in 0.1 ms. It may not be pretty, but it's much faster.


I know some of you will complain that it's VBs fault that it wastes overhead and time instantiating the RegExp library and other "better" languages have it built-in. Who cares? We're talking about losing 0.4 ms of time per execution. So VBs strings are immutable. Just about any program written these days uses a few MB of memory at the least, you're talking about being wasteful because it uses a few more bytes to create new strings on function calls. At what point does it become wasteful to spend this much time devising a barely noticeable performance improvement?

Re: Just in case SPACE isn't supported in the future

2005-08-09 13:07 • by Mung Kee
40316 in reply to 40313
Manni:
At what point does it become wasteful to spend this
much time devising a barely noticeable performance improvement?


At the point that you decided to use VB.



Disclaimer: I'm not a VB-hater.  It's just no Porsche.

Re: Just in case SPACE isn't supported in the future

2005-08-09 13:18 • by Manni
40322 in reply to 40316

Mung Kee:
Manni:
At what point does it become wasteful to spend this much time devising a barely noticeable performance improvement?

At the point that you decided to use VB.

Disclaimer: I'm not a VB-hater.  It's just no Porsche.


If my shitty car gets up to 60 mph in the same time a Porsche can get to 130 mph, I call that a significant difference. If you think you can save a few hundreths of a millisecond by using one language as opposed to another, then I'm not convinced your language is better.


And by the way, I was looking for a real answer, not a smart-assed comment that provided no further insight about when the coding time investment does not yield appropriate performance returns.


"Ha ha VB sux"...yeah, thanks for contributing.

Re: Just in case SPACE isn't supported in the future

2005-08-09 13:37 • by Humble merc coder
40328 in reply to 40263

umm... I wrote it to spec



...And thus begins many a sad tale.

« PrevPage 1 | Page 2Next »

Add Comment