Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

14
  • 1
    "popping" a stack is inherently a two-step algorithm: get first element, and remove it. You did good to implement it in 2 lines. You can put them inside a function to hide it away.
    – Verpous
    Commented Jan 15, 2023 at 20:51
  • 1
    nag ... a queue is a FIFO ... a stack is a LIFO (last-in, first-out)
    – markp-fuso
    Commented Jan 15, 2023 at 21:05
  • do you need this construct to persist across script calls, or perhaps between parallel calls, hence the use of a file? or does the construct only need to persist for the life of a single script call?
    – markp-fuso
    Commented Jan 15, 2023 at 21:06
  • If you know the maximum number of lines you'd store at a time, just use an array and rotate.
    – konsolebox
    Commented Jan 15, 2023 at 21:50
  • 1
    Using a file for this is fairly questionable: unless you get quite fancy, you end up rewriting the whole thing from scratch whenever you need to make any modification. If the queue can be large, you're probably better off with a directory instead of a single file, so you can just create a new file with a n+1 name or read the file with the lowest name. (There are also approaches that require storing a byte offset and seeking to that offset with a tool like dd; you've got the startup time for that tool itself to pay, but it avoids needing to rewrite all the data when popping off the beginning) Commented Jan 15, 2023 at 22:25