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. 1 0:a
    here store 1 @ 0th position of array a.

  2. 0sa
    push 0 stack of register a.

  3. 2 0:a here again store 2 @ 0th position of array a thereby overwriting previous 1 stored @ location.

  4. la
    pop 0 stored on stack of register a , push main stack.

  5. 0;a
    again push 0 main stack , pop use array index , push 2 stored @ 0th location of array a main stack.

  6. 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

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -