r/shittyprogramming • • May 04 '15

// remove numbers

Post image
418 Upvotes

51 comments sorted by

8

u/MeowMeowWoofWoof May 04 '15

This causes me pain. Why am I even looking at this?

25

u/okawei May 04 '15

At least it's well documented. // leaving a comment

89

u/Jack126Guy May 04 '15

A bit further down:

$words = preg_replace('/A/', 'a', $words); // convert to lowercase
$words = preg_replace('/B/', 'b', $words); // convert to lowercase
$words = preg_replace('/C/', 'c', $words); // convert to lowercase
$words = preg_replace('/D/', 'd', $words); // convert to lowercase
// ...

26

u/[deleted] May 04 '15

inb4

$words = preg_replace('', ' ', $words); // replace space 

73

u/[deleted] May 04 '15

Why so complicated? Just put it into one line:

$words = preg_replace('/(?:[^234567890\D]|[^134567890\D]|[^124567890\D]|[^123567890\D]|[^123467890\D]|[^123457890\D]|[^123456890\D]|[^123456790\D]|[^123456780\D]|[^123456789\D]){1,}/', '', $words);

If I'd have more time I'd use negative lookarounds as that's the only way to be sure every digit is found and eliminated!

10

u/SleepyHarry May 04 '15

{1,} turned my gentle giggle into a full-blown snort.

18

u/Meshiest May 04 '15

claps slowly

13

u/globalvarsonly May 05 '15 edited May 05 '15
while () {
  hands->clap;
  $edit++;
  sleep 3;
}

6

u/Meshiest May 05 '15

you're not using any global variables :c

6

u/globalvarsonly May 05 '15

Oops, $edit is now incremented in each loop, and is a global in package "main" by default in my shitty non-strict perl.

2

u/Chondriac May 06 '15

make sure you -- that sleep amount to get the full effect

37

u/[deleted] May 04 '15

I spent a lot of time wondering what the fuck were all those swords.

I'm not used to php.

15

u/ireddit_didu May 05 '15

$words for $200

8

u/corruptio May 05 '15

$words for $ale?

4

u/DrVirite May 05 '15

$wood for $sheep

2

u/critter_chaos May 05 '15

No way am I helping you build a settlement!

17

u/WASDx May 04 '15

What a fool, he should have used a for loop.

9

u/greenwizard88 May 05 '15

Like this?

$letters = range('A', 'Z');
foreach($letters as $letter)
{
    $lower = strtolower($letter);
    $words = preg_replace('/'.$letter.'/', ''.$lower.'', $words); 
}

$numbers = range(0, 9);
foreach($numbers as $number)
{
    $words = preg_replace('/'.$number.'/', '', $words); 
}

16

u/Lopsidation May 05 '15

Wow, what terrible code. This only removes the one-digit numbers; he needs at least 2 billion more lines to get them all.

3

u/Chondriac May 06 '15

also some more comments. wouldn't want to have to scroll up 2 billion lines of code to remember what that line is doing.

14

u/Prawny May 04 '15

Dear Lord.

9

u/TheWheez May 05 '15

Hey please don't hurt me for asking but I'm trying to figure out what the problem is here. Is it a PHP thing or just the fact that he's hardcoding this for every respective number?

18

u/recursive May 05 '15

The problem is that there are much better ways of doing this. If you use regular expression (he is), you can do it in one statement. If you're going to replace every character separately, you don't need the overhead of regular expressions.

7

u/TheWheez May 05 '15 edited May 05 '15

Ah I guess I didn't realize preg_replace was for regular expressions.

Thanks!

39

u/annoyed_freelancer May 04 '15
$words = preg_replace('/[0-9]/', '', $words);

46

u/[deleted] May 04 '15
$words = preg_replace('/\d/', '', $words);

Get on my level

8

u/[deleted] May 05 '15
$words = preg_replace('/\d/gm', '', $words);

FTFY

15

u/Meshiest May 04 '15

that would be a step down

19

u/poop-trap May 04 '15

I have an even better solution.

$code = preg_replace($php, NULL, $code);

7

u/[deleted] May 05 '15
die();

1

u/theufomusic May 05 '15

Then: chain "chuckieegg"

3

u/Astrokiwi May 05 '15
  from NumberRemover import removeNumbers
  
  words = removeNumbers(words)

2

u/Chondriac May 06 '15
words = words.removeNumbersFromWords(words=words)

what even are words

-11

u/[deleted] May 04 '15

Why is it that programmers always feel the need to fix everything they see? That was precisely the first thing I've thought of when I saw the code.

46

u/bakmanthetitan329 May 04 '15

Not being a professional programmer, I can say that I've been confused several times as to what is wrong with some code on this sub. I always appreciate people using this sub constructively.

8

u/[deleted] May 04 '15

I'm not saying it's a bad thing, it's just something I noticed. That was actually a genuine question.

6

u/IWentToTheWoods May 05 '15

I noticed something similar with FizzBuzz. The first articles to popularize it just mentioned it in passing, since the point of those articles was how many people applying for programming jobs couldn't write a simple program. Instead of addressing that topic, the comments were full of people saying "here's how I'd write it in $language!"

I suppose it's a form of nerd sniping, where none of us can concentrate when there's a puzzle at hand.

1

u/thedboy May 05 '15

I'd like to see Malbolge FizzBuzz. At an interview.

0

u/ababcock1 May 05 '15

If you find yourself copy pasting significant amounts of code you are almost certainly doing it wrong.

4

u/amazing_rando May 04 '15

I don't think it's specific to programmers. I write a lot on the side, and I find myself mentally correcting other people's grammar & editing for poor word choice about as often as I find myself trying to fix bad code.

I'm less likely to post it as a comment, though. I figure a lot of people use this subreddit as a learning source.

3

u/Jonno_FTW May 05 '15

This place should actually be called /r/codereview

2

u/Felz May 04 '15

Professional programmer here- ugly code often hides bugs, so it's either fix it now or spend hours finding it was a problem and fixing it later. More importantly, elegance makes me happy.

0

u/paincoats May 05 '15

people just showing off i think

5

u/poop-trap May 04 '15
$employee = preg_replace($employment_status, NULL, $employee);

3

u/[deleted] May 05 '15

$employee = preg_replace($employment_status, "be paid per lines of code", $employee);

2

u/tajjet May 05 '15

I liked the part when they removed a digit

2

u/t3hcoolness May 05 '15

im 10 and what is regular expressions

3

u/seriouslulz May 04 '15
for ($i = 0; $i < 10; $i++) {
    $words = str_replace(strval($i), '', $words);
}

1

u/knubbze May 05 '15

Looks like a serious case of overly zealous use of Ctrl-D

1

u/dsiOneBAN2 May 12 '15

How does one manage to learn just the right amount of regex to do this but not a better way?