日期:2014-05-16  浏览次数:20668 次

一段awk脚本代码 不解 请指教
源代码如下:
[root@kaven ctest]# cat belts.awk
#!/bin/awk -f
BEGIN{
  FS="#"
  belt["Yellow"]
  belt["Orange"]
  belt["Red"]

  student["Junior"]
  student["Senior"]
}

{
  for(colour in belt)
  {
  if( $1 == colour )
  belt[colour]++
  }
}
{
  for(senior_or_junior in student)
  {
  if( $2 == senior_or_junior)
  student[senior_or_junior]++
  }
}

END{
  for( colour in belt )
  print "the club has", belt[colour],colour,"Belts"
  for( senior_or_junior in student)
  print "the club has", student[senior_or_junior],senior_or_junior,"students"
}

[root@kaven ctest]# cat grade_student.txt
Yellow#Junior
Orange#Senior
Yellow#Junior
Purple#Junior
Brown-2#Junior
White#Senior
Orange#Senior
Red#Junior
Brown-2#Senior
Yellow#Senior
Red#Junior
Blue#Senior
Green#Senior
Purple#Junior
White#Junior

[root@kaven ctest]# ./belts.awk grade_student.txt
the club has 2 Red Belts
the club has 2 Orange Belts
the club has 3 Yellow Belts
the club has 7 Senior students
the club has 8 Junior students

请问
(1)belt["Yellow"]
  belt["Orange"]
  belt["Red"]是否是开辟一个belt的数组,数组大小为3

(2)colour in belt 中colour是否是依次取值为Yellow,Orange,Red
但又是如何赋值的?


------解决方案--------------------
1)belt是个哈希表,分别初始以"Yellow", "Orange", "Red"为索引的三个变量,为0
2) 是的
赋值相当于belt["Yellow"]++
------解决方案--------------------
这个不应该理解成数组,因为单元里没有储存数据。

应该理解成enum枚举类型。