dc unix : preserving initial value even after storing new value at the same array index -
i'm unable understand example given in manpage of dc:
$ dc 1 0:a 0sa 2 0:a la 0;ap 1 to me answer should 2 because:
1 0:a
here store 1 @ 0th position of arraya.0sa
push 0 stack of registera.2 0:ahere again store 2 @ 0th position of arrayathereby overwriting previous 1 stored @ location.la
pop 0 stored on stack of registera, push main stack.0;a
again push 0 main stack , pop use array index , push 2 stored @ 0th location of arrayamain stack.p
print top of main stack 2. answer should 2.
what missing?
edit :
$ dc -v dc (gnu bc 1.06.95) 1.3.95
the explanation given before example in man page:
note each stacked instance of register has own array associated it.
in other words, when use array commands , stack commands on same register, create 2-dimensional structure. array commands operate on entries within top row, , stack commands operate on whole rows.
also, "scalar" value of stack entry retrieved l , l commands separate array, not alias 0th element. (i didn't man page, seems true in experiments.)
here commands in example resulting values of a expanded verbose format. outermost structure stack, top listed first.
initial value of registers:
[ { scalar: undefined, array: [] } ] 1 0:a
[ { scalar: undefined, array: [1] } ] 0 sa
[ { scalar: 0, array: [] }, { scalar: undefined, array: [1] } ] 2 0:a
[ { scalar: 0, array: [2] }, { scalar: undefined, array: [1] } ] la
[ { scalar: undefined, array: [1] } ] (the top entry has been popped. scalar value, 0, has been pushed main stack, , array value, [2], has been discarded.)
0;a
[ { scalar: undefined, array: [1] } ] (the ; command doesn't modify register, copies 0th array entry onto main stack. since array [1], puts 1 on top of main stack.)
Comments
Post a Comment