[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
## Re: [bug-gawk] How to mimic inserting an element to a vector?

**From**: |
arnold |

**Subject**: |
Re: [bug-gawk] How to mimic inserting an element to a vector? |

**Date**: |
Thu, 11 Jul 2019 12:34:56 -0600 |

**User-agent**: |
Heirloom mailx 12.5 7/5/10 |

Your way seems to work. I would probably write it like this
(untested):
function insert(a, ind, val, i, l)
{
l = length(a)
for (i = l; i >= ind; i--)
a[i+1] = a[i]
a[ind] = val
}
Arnold
Peng Yu <address@hidden> wrote:
>* Hi,*
>
>* I use the following code to mimic inserting an element to a vector*
>* (i.e, the keys are 1,2,...). But it is a little cumbersome. Is there*
>* any implement that is better?*
>
>* function insert(a, i, v) {*
>* for(k in a) {*
>* if(k>=i) {*
>* a[k+1] = a[k]*
>* }*
>* }*
>* a[i] = v*
>* }*
>
>* BEGIN {*
>* for(i=1;i<=3;++i) {*
>* a[i] = 100 + i*
>* }*
>* PROCINFO["sorted_in"] = "@ind_num_desc"*
>* insert(a, 2, 2)*
>
>* PROCINFO["sorted_in"] = "@ind_num_asc"*
>* for(k in a) {*
>* print k, a[k]*
>* }*
>* }*
>
>* -- *
>* Regards,*
>* Peng*