Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use AWK array

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to use AWK array", the content is simple and easy to understand, organized clearly, I hope to help you solve doubts, let Xiaobian lead you to study and learn "how to use AWK array" this article bar.

AWK can use associative arrays, which can be numbers or strings. AWK associative arrays do not need to be declared in advance because they can be automatically incremented or decremented at runtime.

Array syntax format: array_name[index]=valuearray_name: array name index: array index value: array element assigned value Create array

Let's look at how to create arrays and how to access array elements:

$ awk 'BEGIN {sites["runoob"]="www.runoob.com";sites["google"]="www.google.com"print sites["runoob"] "\n" sites["google"]}'

Execute the above command and the output is:

www.runoob.comwww.google.com

In the example above, we defined an array of sites, the index of the array is the English abbreviation of the site, and the value is the site access address. Array elements can be accessed using the following format:

array_name[index] Delete array elements

We can delete an array element using the delete statement, with the following syntax:

delete array_name[index

In the following example, the google element in the array is deleted (the delete command has no output):

$ awk 'BEGIN {sites["runoob"]="www.runoob.com";sites["google"]="www.google.com"delete sites["google"];print fruits["google"]}'multidimensional arrays

AWK itself does not support multidimensional arrays, but we can easily simulate multidimensional arrays using one-dimensional arrays.

The following example is a 3×3 array:

100 200 300400 500 600700 800 900

In the example above, array0 stores 100, array0 stores 200, and so on. To store 100 at array0, we can use the following syntax: array["0,0"] = 100.

We used 0, 0 as indexes, but these are not two index values. In fact, it is a string index of 0,0.

Here is an example of simulating a two-dimensional array:

$ 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;#Output 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"];}'

Executing the above command results in the following:

array[0,0] = 100array[0,1] = 200array[0,2] = 300array[1,0] = 400array[1,1] = 500array[1,2] = 600

There are many operations that can be performed on arrays, such as sorting array elements using asort, sorting array indexes using asorti, and so on.

The above is "AWK array how to use" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report