Giter Site home page Giter Site logo

Deleting from array while looping about goawk HOT 2 CLOSED

n8ta avatar n8ta commented on May 23, 2024
Deleting from array while looping

from goawk.

Comments (2)

benhoyt avatar benhoyt commented on May 23, 2024

Interesting! Yeah, the spec doesn't seem to define this, though I think a reasonable assumption (extrapolating the bit you quoted about adding new elements) would be that it's not specified and the implementation is free to yield the deleted element (gawk, mawk), or not yield it (GoAWK, original-awk).

Note that apart from the question of how delete interactions with iteration, there's also the question of iteration order itself. The POSIX AWK spec says that the implementation can iterate "in an unspecified order". GoAWK does just that, and actually uses Go's map iteration order, which varies from iteration to iteration. In this case for whatever reason Go usually iterates 0 first, then 1 ... but if you run it a dozen times or so, you'll see it iterate the other way:

# case where 0 is iterated first (seems to be more common)
$ goawk -f t.awk
key 0 val zero
# every so often 1 is iterated first
$ goawk -f t.awk
key 1 val one
key 0 val zero

GoAWK also uses Go's delete() semantics, which is that "If a map entry that has not yet been reached is removed during iteration, the corresponding iteration value will not be produced." In other words, if the 0 is iterated first, the 1 will be deleted and not shown at all; but if the 1 is iterated first, GoAWK will try to delete 2, which doesn't exist so it'll do nothing, and then show the 0 after.

Note also that original-awk ("one true awk") seems to iterate the 0 first, and delete the 1:

$ original-awk -f t.awk
key 0 val zero

In any case, given the above, I think GoAWK's behaviour is within spec here.

from goawk.

n8ta avatar n8ta commented on May 23, 2024

Yes I did notice goawk is the only one to iterate in an (obviously) varying order but that's completely fair given the spec and seems natural for a go impl. If onetrueawk agrees with you I'm a lot more confident that this a fair interpretation of the spec (:

I really don't like the gawk/mawk behavior of yielding a deleted key; happy you did not got go that way.

from goawk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.