Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Curious: How do you do “every other row” in a loop if you don’t do modulo?


Not advocating for being oblivious about modulo op, but you could use stepping in some languages. Example, in Kotlin: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.ranges/s...


Why would you want to do every other row in a database? That just sounds like "a random half" with extra steps if you're doing math on your DBMS's equivalent of RowID.


Things like applying a different colour or styling for visual effect is a common-ish reason, or parsing some input which looks like:

  Stuff I want
  Extra stuff belonging to the above line I want to ignore
  More stuff I want
  More ignore stuff
  [..etc..]
Which can be from a CLI tool, a web scraper tool, something copy pasted from a webpage, or something like that. This data can often be "messy" but in a regular way. I've parsed WikiText tables from Wikipedia like this (very crude, but it only needs to run once).

Another example of "% 2" is for centre aligning strings in a grid (e.g. a terminal; if it's odd you need to add one extra space to one side). I've also used it for time calculations (e.g. printing seconds as "hour:minutes:seconds").

That's from the top of my head; I've probably used it in other things as well. Note this is all high-level application work: no muckery with low-level controllers, firmware, or whatnot.

Is the % operator very common? No; but I don't think it's as "virtually never happens"-uncommon as was mentioned a few posts up the thread.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: