免费注册 查看新帖 |

Chinaunix

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

数组赋值 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-09-25 09:35 |只看该作者 |倒序浏览
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
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
2 [报告]
发表于 2010-09-25 09:48 |只看该作者
&ptid回复 1# baobamboo


    name[1]=aox?

论坛徽章:
0
3 [报告]
发表于 2010-09-25 09:53 |只看该作者
回复 1# baobamboo


   
   < Day Day Up >     

12.7. Arrays
Korn shell arrays are one-dimensional arrays that may contain up to 1,024 (size varies) elements consisting of words or integers. The index starts at 0. Each element of an array can be set or unset individually. Values do not have to be set in any particular order. For example, you can assign a value to the tenth element before you assign a value to the first element. An array can be set using the set command with the –A option.

Associative arrays are supported under versions of the Korn shell that are more recent than 1988.

Example 12.50.

(The Command Line)

1    $ array[0]=tom

     $ array[1]=dan

     $ array[2]=bill

2    $ print ${array[0]}     # Curly braces are required.

     tom

3    $ print ${array[1]}

     dan

4    $ print ${array[2]}

     bill

5    $ print ${array
  • }     # Display all elements.

         tom dan bill

    6    $ print ${#array
  • }    # Display the number of elements.

         3




    EXPLANATION

    The first three elements of the array are assigned values. The index starts at 0.

    The value of the first array element, tom, is printed. Make sure you remember to surround the variable with curly braces. $array[0] would print tom[0].

    The value of the second element of the array, dan, is printed.

    The value of the third element of the array, bill, is printed.

    All elements in the array are printed.

    The number of elements in the array is printed. An array can be declared with typeset if you know the size and type.

    Example 12.51.

    (At The Command Line)

    1    $ typeset –i ints[4]     # Declare an array of four integers.

    2    $ ints[0]=50

         $ ints[1]=75

         $ ints[2]=100

    3    $ ints[3]=happy

         ksh: happy: bad number




    EXPLANATION

    The typeset command creates an array of 4 integers.

    Integer values are assigned to the array.

    A string value is assigned to the fourth element of the array, and the Korn shell sends a message to standard error.

    12.7.1 Creating Arrays with the set Command
    You can assign the values of an array using the set command. The first word after the –A option is the name of the array; the rest of the words are the elements of the array.

    Example 12.52.

    (The Command Line)

    1   $ set –A fruit apples pears peaches

    2   $ print ${fruit[0]}

        apples

    3   $ print ${fruit
  • }

        apples pears peaches

    4   $ fruit[1]=plums

    5   $ print ${fruit
  • }

        apples plums peaches




    EXPLANATION

    The set command with the –A option creates an array. The name of the array, fruit, follows the –A option. Each of the elements of the fruit array follow its name.

    Subscripts start at 0. Curly braces are required around the variable for it to be evaluated properly. The first element of the array is printed.

    When the asterisk is used as a subscript, all elements of the array are displayed.

    The second element of the array is reassigned the value plums.

    All elements of the array are displayed.

       < Day Day Up >     
  • 论坛徽章:
    0
    4 [报告]
    发表于 2010-09-25 09:55 |只看该作者
    楼主是想把那10个人名赋值给数组吗?

    论坛徽章:
    0
    5 [报告]
    发表于 2010-09-25 10:05 |只看该作者
    提示: 作者被禁止或删除 内容自动屏蔽

    论坛徽章:
    0
    6 [报告]
    发表于 2010-09-25 10:46 |只看该作者
    提示: 作者被禁止或删除 内容自动屏蔽

    论坛徽章:
    0
    7 [报告]
    发表于 2010-09-25 10:57 |只看该作者
    回复 5# baobamboo


        想用关联数组?
       看例子吧

    1.    awk 'BEGIN{a[xx]="hello";if(xx in a){print a[xx]}}'
    复制代码

    论坛徽章:
    0
    8 [报告]
    发表于 2010-09-25 11:27 |只看该作者
    提示: 作者被禁止或删除 内容自动屏蔽

    论坛徽章:
    0
    9 [报告]
    发表于 2010-09-25 14:57 |只看该作者
    提示: 作者被禁止或删除 内容自动屏蔽

    论坛徽章:
    0
    10 [报告]
    发表于 2010-09-25 17:25 |只看该作者
    回复 9# baobamboo


        你试试吧,没环境
    1. /usr/local/bin/lmstat -c $1 -f $2|awk '
    2.         BEGIN{
    3.                 ML4["caoxy"]=0
    4.                 ML4["gongzz"]=0
    5.                 ML4["liyan"]=0
    6.                 ML4["maz"]=0
    7.                 ML4["wangsd"]=0
    8.                 ML4["wuce"]=0
    9.         }
    10.         {
    11.                 if($1 in ML4)
    12.                 {
    13.                         ML4[$1]++
    14.                 }
    15.         }
    16.         END{
    17.                 for(i in ML4)
    18.                 {
    19.                         print i":"ML4[i]
    20.                 }
    21.         }'
    复制代码
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则 发表回复

      

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

    清除 Cookies - ChinaUnix - Archiver - WAP - TOP