免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2880 | 回复: 7
打印 上一主题 下一主题

谈谈我理解的awk数组 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-09-18 12:52 |只看该作者 |倒序浏览
awk数组是awk的重要技巧点之一。

awk使用的是关联数组,这和c语言的顺序数组大不一样,所以很多初学者刚开始使用的时候不太习惯。

awk的数组既然叫做关联数组,它的本质就是映射。
用数组的时候你把它想象成一个映射。

比如
a[ $1 ] = $2
可以把它想象成 f (x) = y;的形式,也就是 f ($1) = $2;

同样的, a[ $1,  $2 ] = $3;
可以想象成  f(x, y) = z;的形式,两个自变量,一个因变量。

用函数的方式来描述数组,这样似乎容易理解一些。

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
2 [报告]
发表于 2009-09-18 12:57 |只看该作者
说得不错啊,谢谢!

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
3 [报告]
发表于 2009-09-18 13:06 |只看该作者

回复 #1 yy_galois 的帖子

a[x,y]理解成两个自变量。
这样理解不太好吧。

其实,可以简单地把关联数组理解成key/value对。

awk的数组和perl的关联数组(现在称为hash)一样,只是没有后者的功能强大。

论坛徽章:
0
4 [报告]
发表于 2009-09-18 13:15 |只看该作者

回复 #3 blackold 的帖子

hehe, 只是一种记忆方法。

论坛徽章:
0
5 [报告]
发表于 2009-09-18 13:18 |只看该作者
干脆用perl的多维数组写嘛..反正都有perl...

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
6 [报告]
发表于 2009-09-18 13:21 |只看该作者

回复 #4 yy_galois 的帖子

但是,习惯很重要。

慢慢地这种“坏想法”就会不知不觉地影响你,影响你的程序。

就好比你可以当狗称为猪,叫法本身没有什么不可以,但时间长了,问题就出来了。除非你与世隔绝。

论坛徽章:
0
7 [报告]
发表于 2009-09-18 13:34 |只看该作者
习惯很重要,从一开始就要正确的理解

论坛徽章:
0
8 [报告]
发表于 2009-09-18 15:18 |只看该作者
刚看了一段关于awk数组的描述,贴上来:

8.4.1 Associative Arrays

In awk, all arrays are associative arrays. What makes an associative array unique is that its index can be a string or a number.

In most programming languages, the indices of arrays are exclusively numeric. In these implementations, an array is a sequence of locations where values are stored. The indices of the array are derived from the order in which the values are stored. There is no need to keep track of indices. For instance, the index of the first element of an array is "1" or the first location in the array.

An associative array makes an "association" between the indices and the elements of an array. For each element of the array, a pair of values is maintained: the index of the element and the value of the element. The elements are not stored in any particular order as in a conventional array. Thus, even though you can use numeric subscripts in awk, the numbers do not have the same meaning that they do in other programming languages - they do not necessarily refer to sequential locations. However, with numeric indices, you can still access all the elements of an array in sequence, as we did in previous examples. You can create a loop to increment a counter that references the elements of the array in order.

Sometimes, the distinction between numeric and string indices is important. For instance, if you use "04" as the index to an element of the array, you cannot reference that element using "4" as its subscript. You'll see how to handle this problem in a sample program date-month, shown later in this chapter.

Associative arrays are a distinctive feature of awk, and a very powerful one that allows you to use a string as an index to another value. For instance, you could use a word as the index to its definition. If you know the word, you can retrieve the definition.

For example, you could use the first field of the input line as the index to the second field with the following assignment:

    array[$1] = $2

Using this technique, we could take our list of acronyms and load it into an array named acro.

    acro[$1] = $2

Each element of the array would be the description of an acronym and the subscript used to retrieve the element would be the acronym itself.

  ......


8.5.1 Multidimensional Arrays

Awk supports linear arrays in which the index to each element of the array is a single subscript. If you imagine a linear array as a row of numbers, a two-dimensional array represents rows and columns of numbers. You might refer to the element in the second column of the third row as "array[3, 2]." Two- and three-dimensional arrays are examples of multidimensional arrays. Awk does not support multidimensional arrays but instead offers a syntax for subscripts that simulate a reference to a multidimensional array. For instance, you could write the following expression:

    file_array[NR, i] = $i

where each field of an input record is indexed by its record number and field number. Thus, the following reference:

    file_array[2, 4]

would produce the value of the fourth field of the second record.

This syntax does not create a multidimensional array. It is converted into a string that uniquely identifies the element in a linear array. The components of a multidimensional subscript are interpreted as individual strings ("2" and "4," for instance) and concatenated together separated by the value of the system variable SUBSEP. The subscript-component separator is defined as "\034" by default, an unprintable character rarely found in ASCII text. Thus, awk maintains a one-dimensional array and the subscript for our previous example would actually be "2\0344" (the concatenation of "2," the value of SUBSEP, and "4"). The main consequence of this simulation of multidimensional arrays is that the larger the array, the slower it is to access individual elements. However, you should time this, using your own application, with different awk implementations (see Chapter 11, A Flock of awks).

        .......
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP