• (disco)

    I don't get this query:

    SELECT TOP (1) n = (ROW_NUMBER() OVER (ORDER BY number))-1 FROM [master]..spt_values ORDER BY n

    Looks like spt_values comes from Sybase and SQL Server, but is this always guaranteed to return 0?

  • (disco) in reply to JBert
    JBert:
    I don't get this query:

    I didn't understand much of it either... and I've been writing queries for Discourse (well our instance of it anyway):

    https://meta.discourse.org/t/badges-request-for-reason-field-on-manual-badge-assignment/17625/7?u=pjh

  • (disco) in reply to JBert

    Because for number starts at 1 and the query subtracts one form the route number....... That's always going to be zero.

  • (disco)

    haha Perl, remember when i had to port a perl script once... True headache lol

    [image]
  • (disco) in reply to JBert
    JBert:
    is this always guaranteed to return 0

    Yes

    PJH:
    I didn't understand much of it either

    As intended :)

    accalia:
    That's always going to be zero.

    You got it :)

  • (disco)

    Ah - think I've got it...

    my $mysql = 'SELECT TOP (1) n = (ROW_NUMBER() OVER (ORDER BY number))-1 FROM [master]..spt_values ORDER BY n';
    my $mysth = $dbh->prepare($sql);
    $mysth->execute(1, 10);
    while (my @row = $sth->fetchrow_array) {
       $OurZero= $row[0];
    }
    
    • ROW_NUMBER() starts at one
    • ROW_NUMBER() OVER (ORDER BY number) and increments one by each number that appears
    • (ROW_NUMBER() OVER (ORDER BY number))-1 makes it zero-based instead of one-based
    • n = (ROW_NUMBER() OVER (ORDER BY number))-1 allows this incrementing 0-based number to become the first column of each row alised as n
    • TOP (1) n = (ROW_NUMBER() OVER (ORDER BY number))-1 but only the first row is selected

    so..

    • while (my @row = $sth->fetchrow_array) { will only ever iterate once, with the first (and only) row and
    • $OurZero= $row[0]; selects the first column out of the one row returned, which pointed out above is the 1-based row number minus one

    which can only ever return 0.

    Yuck.

  • (disco) in reply to PJH

    A+! I believe the ROW_NUMBER OVER weirdness is because of MSSQL special syntax. [master]..spt_values is meta table which is useful for generating a list of numbers, and also has some actual meaning nobody uses it for.

  • (disco) in reply to Yamikuronue
    Yamikuronue:
    I believe the ROW_NUMBER OVER weirdness is because of MSSQL special syntax.

    Postgres has it as well (if you can spot it here that is...)

    Our 5% Poster badge:

    WITH exclusions AS ( /* Which topics to exclude from counters */
        SELECT user_id, id, topic_id, post_number
        FROM posts
        WHERE raw LIKE '%[UUID omitted for obvious reasons]%' AND
        user_id IN  (
            SELECT gu.user_id
            FROM group_users gu
            WHERE group_id IN(
                SELECT g.id
                FROM groups g
                WHERE g.name IN ('admins')
            )
        )
    ),
    LastMonth AS ( /* Count eligible posts from last month */
        SELECT row_number() OVER (ORDER BY count(*) DESC, bp.user_id), u.username, bp.user_id, count(*)
        FROM badge_posts bp
        JOIN users u on u.id=bp.user_id AND
            bp.user_id NOT IN ( /* ignore bots */
                    SELECT gu.user_id
                    FROM group_users gu
                    WHERE group_id IN(
                        SELECT g.id
                        FROM groups g
                        WHERE g.name IN ('bots')
                    )
            )
        WHERE topic_id NOT IN ( /* short topics */
           SELECT topic_id FROM badge_posts GROUP BY topic_id HAVING count(topic_id) <10
        ) AND topic_id NOT IN ( /* Ineligible topics */
           SELECT topic_id
           FROM exclusions
        ) AND bp.created_at > CURRENT_DATE - INTERVAL '1 month'
        GROUP BY u.username, bp.user_id
        HAVING count(*) > 1
        ORDER BY count(*) DESC, bp.user_id
    ),
    TotalUsers AS (
        SELECT max(row_number) from LastMonth
    )
    SELECT username, user_id, row_number, (row_number*100/TotalUsers.max), count, CURRENT_DATE granted_at
    FROM LastMonth, TotalUsers
    WHERE row_number < TotalUsers.max *.05 and row_number != 1
    

    Whether this, of itself, is FP worthy, I'm not too sure...

  • (disco)

    I once had to maintain 5KLOC of perl (a telephone billing app).

    When I got it, there was one comment in the codebase:

    # set to 1 for no f***up
    $rt = 0
    

    Of course, it didn't say f***up, but I'm following the community guidelines.

  • (disco)

    Ah. It's a speed-up loop. Perhaps said perl-slinging chimps were not entirely stupid but in fact rather cunning, after all?

  • (disco)

    I think it's a bit unfair to blame this on perl. TRWTF would have been exactly the same no matter what language the mentally challenged chimpanzees wrapped around that query.

  • (disco)

    Just as he checked his revision into the repository, a coworker popped his head into David's cube. "David, you will not BELIEVE how they're calculating dates!" she gushed.

    I'm not sure how to interpret this. Did the coworker popped David's head into David's cube (popped as in placed? as in punched? as in popping a balloon? as in popping the stack?), or does their payroll system have three genders: Female, Male and FILE_NOT_FOUND?

  • (disco) in reply to Paddles
    Paddles:
    FILE_NOT_FOUND

    Tumblr.

  • (disco) in reply to radio4fan
    radio4fan:
    Of course, it didn't say f***up, but I'm following the community guidelines.

    Which community's guidelines?

  • (disco)
    my $mysql = 'SELECT TOP (1) n = (ROW_NUMBER() OVER (ORDER BY number))-1 FROM [master]..spt_values ORDER BY n';
    

    That's not even valid MySQL syntax! Never name your variables my-anything in any real code!

    my $mysth = $dbh->prepare($sql);
    

    Where did this $sql variable come from? It was $mysql a moment ago.

    while (my @row = $sth->fetchrow_array) {
       $OurZero= $row[0];
    }
    

    Same... ($sth?)

    Though this $OurZero smells like a global variable, which could be modified somewhere else and then

    return ($ReturnValue * $OurZero);
    

    Would no longer be a no-op.

    Also,

    Four hours later, he reached the bottom of the method.

    Seems like a WTF. Wouldn't you at least skim the sub before doing the hardcore optimisation?

    Filed Under: Needs more regexp.

  • (disco) in reply to boomzilla
    boomzilla:
    Which community's guidelines?

    I was going to say maybe the original Jeff FAQ, but /faq now redirects to our early attempt at a custom FAQ, so I'm not even sure how someone would find that mealy-mouthed treatise on bland, "civilized" behavior.

  • (disco) in reply to Paddles

    I've had some apparently androgynous coworkers before.

  • (disco) in reply to HardwareGeek
    HardwareGeek:
    that mealy-mouthed treatise on bland, "civilized" behavior

    That thing is worse than failure. As it's an article with perl, would have been better to bowdlerize with some line noise:

    # set to 1 for no [email protected]
    $rt = 0
    
  • (disco) in reply to HardwareGeek
    HardwareGeek:
    I was going to say maybe the original Jeff FAQ, but /faq now redirects to our early attempt at a custom FAQ, so I'm not even sure how someone would find that mealy-mouthed treatise on bland, "civilized" behavior.

    It's at http://what.thedailywtf.com/t/faq-guidelines/1897 but since that's a direct link to it in Staff not many of you can see it.

    But it's the stock offering that could be found (mutatis mutandis) at https://meta.discourse.org/faq - it certainly hasn't got an edit history.

  • (disco) in reply to radio4fan
    radio4fan:
    I'm following the community guidelines

    Wait, we have community guidelines? I thought we just had Bert Glanstron to enforce proper decorum.

  • (disco) in reply to EatenByAGrue

    Here:

    system:
    Q. I'm new here, what should I do to fit in? A: Follow these simple rules guidelines and you should be ok:

    Wind other forum members up, it's great when you induce a hissy fit in somebody and then sit back and watch the carnage. Seriously though, this isn't like other forums. There's no need to play nice, we're all big boys here. Always file your posts Develop a healthy hatred of markdown


    Filed under: [I HATE MARKDOWN](#tag)
  • (disco) in reply to DaveK
    DaveK:
    Ah. It's a speed-up loop. Perhaps said perl-slinging chimps were not entirely stupid but in fact rather cunning, after all?
    I was about to propose the same theory. But does one really need to bludgeon a database in the process?
  • (disco)

    Relevant entry from my quote file:

    monochrom: some kind of lazy evaluation is already known to highschool kids. teachers tell you that in a*(b+c), "evaluate b+c first", right? well, I challenge you to take 0*(389238493+97283748) and find one single student who faithfully evaluate 389238493+97283748 first.

  • (disco) in reply to antiquarian

    Should I feel terrible that I did not see the multiplication of zero until after performing the addition? My LR>PEMDAS parser didn't even mention there was a multiplication at all until the sixth operation!...

    Load a = 389,238,493
    Load b = 97,283,748
    Add a,b
    Load a = d
    Load b = 0
    Multiply a,b
    Return
    
  • (disco) in reply to antiquarian

    Reminds me of the question my Maths teacher asked after we'd learned binomial expansions:

    Expand (x-a)(x-b)(x-c)...(x-z)

    [spoiler] It's 0 because there's an implied (x-x) in there [/spoiler]

  • (disco) in reply to HardwareGeek
    HardwareGeek:
    I think it's a bit unfair to blame this on perl. TRWTF would have been exactly the same no matter what language the mentally challenged chimpanzees wrapped around that query.

    It's the same association that goes along with VB (pre-.NET). VB itself isn't completely horrible, but most of the people that use it usually are.

  • (disco) in reply to Jaloopa
    Jaloopa:
    Reminds me of the question my Maths teacher asked after we'd learned binomial expansions:

    Expand (x-a)(x-b)(x-c)...(x-z)

    [spoiler] It's 0 because there's an implied (x-x) in there [/spoiler]

    That was the Car Talk puzzler a few weeks ago. Well, at least something similar:

    (x+a)(x-b)(x+c)...(x-z)

  • (disco) in reply to chubertdev

    The trouble with a CodeSOD is that often, there's not enough code submitted to explain the headaches the narrative that came with the code was trying to get across. The OP implied that unreadable perl led to many unspoken wonders of WTFness that I kinda wish s/he'd submitted as well. Alas, I only had the one example, and it was only half complete, so I had to translate some text into code to make it understandable :)

  • (disco) in reply to Yamikuronue
    Yamikuronue:
    The trouble with a CodeSOD is that often, there's not enough code submitted to explain the headaches the narrative that came with the code was trying to get across. The OP implied that unreadable perl led to many unspoken wonders of WTFness that I kinda wish s/he'd submitted as well. Alas, I only had the one example, and it was only half complete, so I had to translate some text into code to make it understandable :)

    I do feel bad for the authors when it's a CodeSOD, and the code submitted isn't something that you can just immediately post.

  • (disco)

    After this morning, I'd like to go on record and say that I like Perl more than Linq.

  • (disco) in reply to chubertdev
    chubertdev:
    After this morning, I'd like to go on record and say that I like Perl more than Linq.

    Both have the property of being amazing when they work…

  • (disco) in reply to dkf
    dkf:
    Both have the property of being _amazing_ when they work…

    Yes, it's pretty amazing when either work.

  • (disco)

    Better ending:

    Just as he checked his revision into the repository, a coworker popped his head into David's cube. "It's your kids, David! Something's gotta be done about your kids!"

  • (disco)

    Perl is wonderful. Most people cannot use it properly. It isn't a language you point an idiot or a code neophyte at, that's for sure.

    I worked for a company that dismissed Perl because some dufus needed to parse the Unix account info (ypcat) on a system with thousands of user id's. IIRC, he ran the ypcat thousands of times, when running it once, and then putting results in a hash would have done nicely. And been very fast.

    You cannot blame the tool when the people slinging the code are completely clueless. But this single "experiment" doomed Perl in the department I worked in a VERY large company. But they trusted the opinion of this programmer doing his very first Perl project. Unfortunately.

    I have been slinging Perl since it came out (1992?). It really is powerful, and not nearly as bad as its reputation with the general public. It can be very readable, and very maintainable.

    Don't toss the baby out with the bathwater.

  • (disco) in reply to HardwareGeek

    But blaming it on perl is the hipster thing to do.

  • (disco) in reply to ElBob

    When I see someone saying that perl is unreadable, I hear "I CAN'T UNDERSTAND REGULAR EXPRESSIONS".

  • (disco) in reply to chubertdev
    chubertdev:
    Yes, it's pretty amazing when either work.

    Huh? LINQ is as straightforward as it gets, especially when you're using it with extension methods. Now, things like LINQ to Entities can and will fail spectacularly if you don't know what you're doing (and because Entity is a bit of a clusterfuck in general), but it's like blaming SQL for the shittiness of Oracle databases.

    coyo:
    When I see someone saying that perl is unreadable, I hear "I CAN'T UNDERSTAND REGULAR EXPRESSIONS".

    I CAN'T UNDERSTAND REGULAR EXPRESSIONS. Seriously, whoever thought that syntax up should be shot with extreme prejudice. It kinda works for simple matches, but then you try something even a bit more complex and inevitably end up with what looks like the results of banging your head on your keyboard for a prolonged period of time.

  • (disco) in reply to antiquarian
    antiquarian:
    I challenge you to take 0*(389238493+97283748) and find one single student who faithfully evaluate 389238493+97283748 first.

    Facebook is FULL of people who do that, or something similar, only with simpler problems like "solve 0 * 7 + 7 - 7"

  • (disco) in reply to Maciejasjmj
    Maciejasjmj:
    Huh? LINQ is as straightforward as it gets, especially when you're using it with extension methods. Now, things like LINQ to Entities can and will fail spectacularly if you don't know what you're doing (and because Entity is a bit of a clusterfuck in general), but it's like blaming SQL for the shittiness of Oracle databases.

    I do agree that at its most basic, it's very easy to understand.

    In the wild, real world, it can spawn its own WTFs.

  • (disco) in reply to FrostCat
    FrostCat:
    Facebook is FULL of people who do that, or something similar, only with simpler problems like "solve 0 * 7 + 7 - 7"

    They'll get that one correct. It's "7 + 7 * 0 + 7" they'll mess up. (I ignore those now - they're boringly the same.)

  • (disco) in reply to FrostCat
    FrostCat:
    "solve 0 * 7 + 7 - 7"

    Without the parens your example misses the shortcut that you were replying to. Remember your order of operations (yes it still evaluates to 0 but the point was the shortcut around what is multiplied by 0).

  • (disco) in reply to chubertdev
    chubertdev:
    In the wild, real world, it can spawn its own WTFs.

    Well, that's true for every tech.

    And I still think that even the worst LINQ atrocities are more readable than average Perl code. But that's just, like, my opinion, man.

  • (disco) in reply to Maciejasjmj
    Maciejasjmj:
    Well, that's true for every tech.

    And I still think that even the worst LINQ atrocities are more readable than average Perl code. But that's just, like, my opinion, man.

    It was more an issue about behavior than readability.

  • (disco) in reply to locallunatic
    locallunatic:
    Without the parens your example misses the shortcut that you were replying to.

    The shortcut was just a particular form. I meant in more general terms. I mean, people will actually get wrong the thing I posted, in the form I posted it. "It's, uh, 23, right?" "No, it's -7!" And then they argue with the person who tells them what the right answer is because they simply don't understand precedence, and evaluate the entire thing left-to-right.

  • (disco) in reply to FrostCat
    FrostCat:
    they simply don't understand precedence, and evaluate the entire thing left-to-right.

    But you can evaluate it left-to-right and arrive at the correct answer, since the multiplication is first? Or am I lacking caffeine again?

    If you really want to throw a bait, try something among the lines of 6 / 2(1 + 2). Hilarity ensues every time.

  • (disco) in reply to Maciejasjmj
    Maciejasjmj:
    But you can evaluate it left-to-right and arrive at the correct answer

    Look, I didn't even bother to work out the answer, I just made up something that looked sort of what I was after, because the actual answer wasn't important.

  • (disco) in reply to Maciejasjmj
    Maciejasjmj:
    6 / 2 (1 + 2)

    We really need a better syntax for that kind of shit.

  • (disco) in reply to Onyx
    Onyx:
    We really need a better syntax for that kind of shit.

    We should've moved to RPN a long time ago, I say.

  • (disco)

    The RealWTF for me is when he doesn't delete the call to CalculateZero entirely.

  • (disco) in reply to Maciejasjmj
    Maciejasjmj:
    Huh? LINQ is as straightforward as it gets, especially when you're using it with extension methods. Now, things like LINQ to Entities can and will fail spectacularly if you don't know what you're doing (and because Entity is a bit of a clusterfuck in general), but it's like blaming SQL for the shittiness of Oracle databases.

    Guess what it was using... :headdesk:

Leave a comment on “A Shining Perl”

Log In or post as a guest

Replying to comment #:

« Return to Article