I have my array:
array = [1:"PLCH2", 2:"PLCH1", 3:"PLCH2"]
I want to loop on array to create a new array unique of unique values and obtain:
unique = [1:"PLCH2", 2:"PLCH1"]
how can I achieve that ?
EDIT: as per @Ed Morton request, I show below how my array is populated. In fact, this post is the key solution to my previous post.
in my file.txt, I have:
PLCH2:A1007int&PLCH1:D987int&PLCH2:P977L
INTS11:P446P&INTS11:P449P&INTS11:P518P&INTS11:P547P&INTS11:P553P
I use split to obtain array:
awk '{
split($0,a,"&")
for ( i in a ) {
split(a[i], b, ":");
array[i] = b[1];
}
}' file.txt
for ( i in a )will re-arrange your values into a random (hash) order. That's often undesirable which is why I usefor ( i=1; i in a; i++ )instead to ensure I visit the array indices in the same order they appeared in the string that was split into the array. See gnu.org/software/gawk/manual/gawk.html#Scanning-an-Array.for (i = 1; i <= length(a); i++)to do the same (assuming the arrayahas numeric indices, which is true for arrays generated by thesplit()function). I wasn't sure if thei in acondition is in proper order - do you know if it works with all awks? (Well, I mostly care about BWK AWK, mawk, and gawk.)length(a)on each iteration of the loop is time consuming vs a hash lookup and is non-portable (per POSIX,length()operates on strings, not arrays).i in ain this context has nothing (directly) to do with order, it's just a hash lookup testing ifiis an index ofawhich will fail whenihas the value 1 more than the max index. Yes, it works in all awks, unlikelength(a)which will only work in awks that support calling length on an array.