Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何使用AWK正确打印循环中的关联数组?_Arrays_String_Awk_Substring - Fatal编程技术网

Arrays 如何使用AWK正确打印循环中的关联数组?

Arrays 如何使用AWK正确打印循环中的关联数组?,arrays,string,awk,substring,Arrays,String,Awk,Substring,上面是一个数据文件,它有三个字段:姓名、年龄、薪水 我想打印30岁以上和30岁以下的人的平均工资、人数和姓名 在本练习中,我想练习使用字符串作为下标 这是我的AWK代码: Beth 45 0 Danny 33 0 Thomas 22 40 Mark 65 100 Mary 29 121 Susie 39 76.5 Joey 51 189.52 Peter 23 78.26 Maximus 34 289.71 Rebecca 21

上面是一个数据文件,它有三个字段:姓名、年龄、薪水

我想打印30岁以上和30岁以下的人的平均工资、人数和姓名

在本练习中,我想练习使用字符串作为下标

这是我的AWK代码:

Beth    45  0
Danny   33  0
Thomas  22  40  
Mark    65  100 
Mary    29  121 
Susie   39  76.5
Joey    51  189.52
Peter   23  78.26
Maximus 34  289.71
Rebecca 21  45.79
Sophie  26  28.44
Barbara 24  107.36
Elizabeth   35  105.69
Peach   40  102.69
Lily    41  123 
输出对我来说很难理解

我预期的应该是这样的:

The average salary of age equals or above 30 is 109.679
There are 9 people age equals or above 30
Their names are Beth    Danny   Mark    Susie   Joey    Maximus Elizabeth   Peach   Lily    
********************************************************
The average salary of age below 30 is 70.1417
There are 6 people age below 30
Their names are Thomas  Mary    Peter   Rebecca Barbara Sophie  
********************************************************
The average salary of age equals or above 30 is 109.679
There are 9 people age equals or above 30
Their names are Beth    Danny   Mark    Susie   Joey    Maximus Elizabeth   Peach   Lily    
********************************************************
The average salary of age below 30 is 70.1417
There are 6 people age below 30
Their names are Thomas  Mary    Peter   Rebecca Barbara Sophie  
********************************************************
The average salary of age equals or above 30 is 109.679
There are 9 people age equals or above 30
Their names are Beth    Danny   Mark    Susie   Joey    Maximus Elizabeth   Peach   Lily    
********************************************************
The average salary of age below 30 is 70.1417
There are 6 people age below 30
Their names are Thomas  Mary    Peter   Rebecca Barbara Sophie  
********************************************************
The average salary of age equals or above 30 is 109.679
There are 9 people age equals or above 30
Their names are Beth    Danny   Mark    Susie   Joey    Maximus Elizabeth   Peach   Lily    
********************************************************
The average salary of age below 30 is 70.1417
There are 6 people age below 30
Their names are Thomas  Mary    Peter   Rebecca Barbara Sophie  
********************************************************
所以我的第一个问题是:我哪里理解错了

我的第二个问题是: 我其实不需要这么多的循环。我只需要

The average salary of age equals or above 30 is 109.679
There are 9 people age equals or above 30
Their names are Beth    Danny   Mark    Susie   Joey    Maximus Elizabeth   Peach   Lily    
********************************************************
The average salary of age equals or above 30 is 109.679
There are 9 people age equals or above 30
Their names are Thomas  Mary    Peter   Rebecca Barbara Sophie  
********************************************************
The average salary of age equals or above 30 is 109.679
There are 6 people age below 30
Their names are Beth    Danny   Mark    Susie   Joey    Maximus Elizabeth   Peach   Lily    
********************************************************
The average salary of age equals or above 30 is 109.679
There are 6 people age below 30
Their names are Thomas  Mary    Peter   Rebecca Barbara Sophie  
********************************************************
The average salary of age below 30 is 70.1417
There are 9 people age equals or above 30
Their names are Beth    Danny   Mark    Susie   Joey    Maximus Elizabeth   Peach   Lily    
********************************************************
The average salary of age below 30 is 70.1417
There are 9 people age equals or above 30
Their names are Thomas  Mary    Peter   Rebecca Barbara Sophie  
********************************************************
The average salary of age below 30 is 70.1417
There are 6 people age below 30
Their names are Beth    Danny   Mark    Susie   Joey    Maximus Elizabeth   Peach   Lily    
********************************************************
The average salary of age below 30 is 70.1417
There are 6 people age below 30
Their names are Thomas  Mary    Peter   Rebecca Barbara Sophie  
********************************************************
for(工资、计数、姓名)
不起作用。有更好的办法吗

The average salary of age equals or above 30 is 109.679
There are 9 people age equals or above 30
Their names are Beth    Danny   Mark    Susie   Joey    Maximus Elizabeth   Peach   Lily    
********************************************************
The average salary of age equals or above 30 is 109.679
There are 9 people age equals or above 30
Their names are Thomas  Mary    Peter   Rebecca Barbara Sophie  
********************************************************
表示
对于薪资中的每个索引,循环遍历count中的每个索引,同时,对于count中的每个索引,循环遍历name中的每个索引,每次打印“foo”
。因此,如果薪水、计数和姓名各有3个条目,那么您将打印“foo”3*3*3=9次

但它比代码中的更复杂,因为在嵌套循环的每个级别上使用相同的变量来保存每个数组的索引值:

for (x in salary)
    for (y in count)
        for (z in name)
            print "foo"
所以我不确定awk将如何处理它——它甚至可能是未定义的行为

由于所有3个数组都有相同的索引,只需选择其中一个数组并在其索引上循环,然后就可以使用相同的索引访问所有3个数组

for (a in salary)
    for (a in count)
        for (a in name)
$cat tst.awk
{
方括号=“年龄”($2<30?):“等于或高于”)“30”
名称[括号]=(名称中的括号?名称[括号]“\t”:“)$1
计数[括号]++
工资[括号]+=$NF
}
结束{
for(名称中的括号){
打印“的平均工资”,括号,“是”,工资[括号]/count[括号]
打印“有”,计数[括号],“人”,括号
打印“他们的名字是”,名字[括号]
打印“***********************************************************************”
}
}
$awk-f tst.awk文件
30岁或以上的平均工资为109.679
有9人年龄等于或大于30岁
他们的名字是贝丝丹尼马克苏西乔伊马克西姆斯伊丽莎白桃子莉莉
********************************************************
30岁以下的平均工资为70.1417
有6个人年龄在30岁以下
他们的名字是托马斯·玛丽·彼得·丽贝卡·索菲·芭芭拉
********************************************************

如果你将(工资中的)的
删除为(计数中的)
会发生什么?谢谢,这很有效。我还发现for(a in salary)for(a in count)for(a in name),这三个for()中的任何一个都可以解决这个问题。对我来说还是有点奇怪
for (a in salary)
    for (a in count)
        for (a in name)
$ cat tst.awk
{
    bracket = "age " ($2 < 30 ? "under" : "equals or above") " 30"

    names[bracket] = (bracket in names ? names[bracket] "\t" : "") $1
    count[bracket]++
    salary[bracket] += $NF
}
END {
    for (bracket in names) {
        print "The average salary of", bracket, "is", salary[bracket] / count[bracket]
        print "There are", count[bracket], "people",  bracket
        print "Their names are", names[bracket]
        print "********************************************************"
    }
}

$ awk -f tst.awk file
The average salary of age equals or above 30 is 109.679
There are 9 people age equals or above 30
Their names are Beth    Danny   Mark    Susie   Joey    Maximus Elizabeth       Peach   Lily
********************************************************
The average salary of age under 30 is 70.1417
There are 6 people age under 30
Their names are Thomas  Mary    Peter   Rebecca Sophie  Barbara
********************************************************