August 26, 2005

AWK sort array

GAWK provides asort and asorti functions to sort arrays, but it is not useful in sorting arraying while wanting to keep the relationship between the index and the value. Here are two functions, zsort and zsorti, that do this.

Here is the code

#!/bin/gawk -f
function zsort(a,b){
count=0;
delete b;
sort= "LC_ALL=C sort -k2nr -t,"
for (i in a){
printf("%s,%s\n",i,a[i]) |& sort;
}
close(sort, "to")
while ((sort |& getline line) > 0){
split(line,x,",");
count++;
b[count,1]=x[1];
b[count,2]=x[2];
}
close(sort)
return count;
}
function zsorti(a,b){
count =0;
delete b;
sort= "LC_ALL=C sort -k1nr -t,"
for (i in a){
printf("%s,%s\n",i,a[i]) |& sort;
}
close(sort, "to")
while ((sort |& getline line) > 0){
split(line,x,",");
count++;
b[count,1]=x[1];
b[count,2]=x[2];
}
close(sort)
return count;
}
BEGIN{
a["x"]=5; a["y"]=7; a["z"]=6; a["a"]=10;

# sort by the array value
num=zsort(a,b);
for (i=1;i<=num;i++){
printf("%s,%s\n",b[i,1],b[i,2]);
}
print "==============="
# sort by the array index
num=zsorti(a,b);
for (i=1;i<=num;i++){
printf("%s,%s\n",b[i,1],b[i,2]);
}
}


Here is the result:

a,10
y,7
z,6
x,5
===============
a,10
x,5
y,7
z,6

August 22, 2005


2005年华盛顿Potomac大河的桥上

Cherry Blossom of 2005 Posted by Picasa

WordAloud released to sourceforge

I recently created my first open source project: WordAloud. It pronouces an English word and displays its definition. I find it very handy. I hope people will use it.

August 13, 2005

What is True Leadership

This is directly from the book by Suzanne Bates.

Here is the leadership value system articulated by our 293 respondents, in roughly descending order:


  • Integrity. .Responses in this category referred to both business dealings and personal interactions. The words most frequently used by respondents to frame this concept were honesty, integrity,ethics, fairness, candor,sincerity, trustworthiness, and truthfulness .
  • Vision.
  • Listening.
  • Giving feedback. Participants place a priority on giving credit where credit is due, including public praise for a job well done. They cited offering positive feedback when deserved and valuing employees' contributions.
  • Emotional intelligence. Treating people well and having empathy and compassionate highly rated.
  • Communication skills.
  • Knowledge and intelligence.
  • Managerial skills.
  • Follow-through ... Strong leaders are consistently concerned about how things come out, not just how they begin
  • Humility. An authentic leader is humble -- willing to seek information and advice and to admit mistakes when they occur.