当前位置:网站首页>Awk from entry to earth (8) array
Awk from entry to earth (8) array
2022-07-04 08:46:00 【Wonderful binary】
Array
AWK There are data structures like associative arrays , The best feature of this data structure is that its index value does not need to be a continuous integer value . We can use both numbers and strings as the index of the array . besides , Associative arrays do not need to declare their size in advance , Because it can automatically increase or decrease at run time . This chapter will explain AWK How to use arrays .
The following is the syntax format used by the array :
array_name[index]=value
among array_name Is the name of the array ,index Is the array index ,value The value assigned to the element in the array .
Create array
To learn more about arrays , Let's first look at how to create an array and how to access array elements :
[jerry]$ awk 'BEGIN {
fruits["mango"]="yellow";
fruits["orange"]="orange"
print fruits["orange"] "\n" fruits["mango"]
}'
Execute the above command to get the following result :
orange
yellow
In the example above , We define a fruit (fruits) Array , The index of this array is the fruit name , Value is the color of the fruit . You can access array elements in the following format :
array_name[index]
Delete array elements
When inserting elements, we use assignment operators . When deleting array elements , We use delete sentence . As shown below :
delete array_name[index]
In the following example , Array orange The element is deleted ( The delete command has no output ):
[jerry]$ awk 'BEGIN {
fruits["mango"]="yellow";
fruits["orange"]="orange";
delete fruits["orange"];
print fruits["orange"]
}'
Multidimensional arrays
AWK Itself does not support multidimensional arrays , However, we can easily use one-dimensional array simulation to realize multi-dimensional array .
The following example is a 3x3 Three dimensional array of :
100 200 300
400 500 600
700 800 900
In the example above ,array[0][0] Storage 100,array[0][1] Storage 200 , By analogy . In order to be in array[0][0] Store 100, We can use the following syntax :
array["0,0"] = 100
Although in the example , We used 0,0 As index , But these are not two index values . in fact , It is a string index 0,0.
The following is an example of simulating a two-dimensional array :
[jerry]$ awk 'BEGIN {
array["0,0"] = 100;
array["0,1"] = 200;
array["0,2"] = 300;
array["1,0"] = 400;
array["1,1"] = 500;
array["1,2"] = 600;
# print array elements
print "array[0,0] = " array["0,0"];
print "array[0,1] = " array["0,1"];
print "array[0,2] = " array["0,2"];
print "array[1,0] = " array["1,0"];
print "array[1,1] = " array["1,1"];
print "array[1,2] = " array["1,2"];
}'
You can get the following result by executing the above command :
array[0,0] = 100
array[0,1] = 200
array[0,2] = 300
array[1,0] = 400
array[1,1] = 500
array[1,2] = 600
Many operations can be performed on arrays , such as , Use asort Finish sorting array elements , Or use asorti Implement the sorting of array index, etc . We will introduce functions that can operate on arrays in later chapters .
边栏推荐
- Leetcode topic [array] - 121 - the best time to buy and sell stocks
- How to choose solid state hard disk and mechanical hard disk in computer
- awk从入门到入土(14)awk输出重定向
- Basic discipline formula and unit conversion
- ctfshow web255 web 256 web257
- Flutter integrated amap_ flutter_ location
- 没有Kubernetes怎么玩Dapr?
- The second session of the question swiping and punching activity -- solving the switching problem with recursion as the background (I)
- 2022 electrician (intermediate) examination question bank and electrician (intermediate) examination questions and analysis
- 如何通过antd的upload控件,将图片以文件流的形式发送给服务器
猜你喜欢

Comprendre la méthode de détection des valeurs aberrantes des données

System disk expansion in virtual machine

Educational Codeforces Round 115 (Rated for Div. 2)

Guanghetong's high-performance 4g/5g wireless module solution comprehensively promotes an efficient and low-carbon smart grid

SSRF vulnerability exploitation - attack redis

Call Baidu map to display the current position

地平线 旭日X3 PI (一)首次开机细节

Azure ad domain service (II) configure azure file share disk sharing for machines in the domain service

Educational Codeforces Round 119 (Rated for Div. 2)

What should I do if there is a problem with the graphics card screen on the computer
随机推荐
Three paradigms of database design
C#实现一个万物皆可排序的队列
awk从入门到入土(9)循环语句
deno debugger
Convert datetime string to datetime - C in the original time zone
Leetcode topic [array] - 121 - the best time to buy and sell stocks
A method for detecting outliers of data
awk从入门到入土(5)简单条件匹配
DM8 uses different databases to archive and recover after multiple failures
[untitled] 2022 polymerization process analysis and polymerization process simulation examination
A single element in an ordered array
How to solve the problem of computer jam and slow down
Manjaro install wechat
Add log file to slim frame - PHP
Codeforces Round #793 (Div. 2)(A-D)
Openfeign service interface call
ArcGIS application (XXII) ArcMap loading lidar Las format data
Laravel page load problem connection reset - PHP
SSRF vulnerability exploitation - attack redis
Call Baidu map to display the current position