更改作为参数数组副本的数组的值

时间:2017-04-02 02:39:19

标签: arrays bash sh

我正在尝试更改数组中的值,该数组是参数数组("$@")的副本。我们假设我按如下方式执行脚本:$ sh script.sh 1 2 3

这是剧本:

list="$@"
echo ${list[*]}
list[1]=4
echo ${list[*]}

预期产出:

1 2 3
1 4 3

我真正得到的是:

1 2 3
1 2 3 4

知道导致此行为的原因以及如何解决?

2 个答案:

答案 0 :(得分:1)

listlist=("$@")设置为普通变量,而不是数组。使用"${list[@]}"将其存储为数组。顺便说一句,你通常应该使用${list[*]}来获取数组的元素,而不是copyarray=("${oldarray[@]}") 来避免元素中的空格问题,通配符扩展到匹配文件列表等等。

通常,这是复制数组的正确方法:

 import json
    from pprint import pprint
    xa1_min = 144.700000
    xa1_max = 144.850000
    ya1_min = -37.650000
    ya1_max = -37.500000

    xa2_min = 144.850000
    xa2_max = 145.000000
    ya2_min = -37.650000
    ya2_max = -37.500000

    xa3_min = 145.000000
    xa3_max = 145.150000
    ya3_min = -37.650000
    ya3_max = -37.500000

    xa4_min =145.150000
    xa4_max =145.300000
    ya4_min =-37.650000
    ya4_max =-37.500000

    xb1_min =144.700000
    xb1_max =144.850000
    yb1_min =-37.800000
    yb1_max =-37.650000

    xb2_min =144.850000
    xb2_max =145.000000
    yb2_min =-37.800000
    yb2_max =-37.650000

    xb3_min =145.000000
    xb3_max =145.150000
    yb3_min =-37.800000
    yb3_max =-37.650000

    xb4_min =145.150000
    xb4_max =145.300000
    yb4_min =-37.800000
    yb4_max =-37.650000

    xc1_min =144.700000
    xc1_max =144.850000
    yc1_min =-37.950000
    yc1_max =-37.800000

    xc2_min =144.850000
    xc2_max =145.000000
    yc2_min =-37.950000
    yc2_max =-37.800000

    xc3_min =145.000000
    xc3_max =145.150000
    yc3_min =-37.950000
    yc3_max =-37.800000

    xc4_min =145.150000
    xc4_max =145.300000
    yc4_min = -37.950000
    yc4_max = -37.800000

    xc5_min =145.300000
    xc5_max =145.450000
    yc5_min =-37.950000
    yc5_max =-37.800000

    xd3_min =145.000000
    xd3_max =145.150000
    yd3_min =-38.100000
    yd3_max =-37.950000

    xd4_min =145.150000
    xd4_max =145.300000
    yd4_min =-38.100000
    yd4_max =-37.950000

    xd5_min =145.300000
    xd5_max =145.450000
    yd5_min =-38.100000
    yd5_max =-37.950000
    #["properties"]['ID']
    # Reading from grid
    with open('tinyTwitter.json') as load_f:
        info = json.load(load_f)

    coordinates = list()
    #print info

    for line in info:
        #print line['json']['coordinates']['coordinates']
        coordinates.append(line['json']['coordinates']['coordinates'])

    #print coordinates[0][1]
    #print coordinates[0][0]

    counts = {'A1':0,'A2':0,'A3':0,'A4':0,'B1':0,'B2':0,'B3':0,'B4':0,'C1':0,'C2':0,'C3':0,'C4':0,'C5':0,'D3':0,'D4':0,'D5':0,'other':0}
    #comparing
    def compare(x,y):


    if xa1_min <= x <= xa1_max and ya1_min <= y <= ya1_max:
        counts['A1'] +=1

    elif xa2_min <= x <= xa2_max and ya2_min <=y<= ya2_max:
        counts['A2'] +=1

    elif xa3_min <= x <= xa3_max and ya3_min <=y<= ya3_max:
        counts['A3'] +=1

    elif xa4_min <= x <= xa4_max and ya4_min <=y<= ya4_max:
        counts['A4'] += 1

    elif xb1_min <= x <= xb1_max and yb1_min <=y<= yb1_max:
        counts['B1'] += 1

    elif xb2_min <= x <= xb2_max and yb2_min <=y<= yb2_max:
        counts['B2'] += 1

    elif xb3_min <= x <= xb3_max and yb3_min <=y<= yb3_max:
        counts['B3'] += 1

    elif xb4_min <= x <= xb4_max and yb4_min <=y<= yb4_max:
        counts['B4'] += 1

    elif xc1_min <= x <= xc1_max and yc1_min <=y<= yc1_max:
        counts['C1'] += 1

    elif xc2_min <= x <= xc2_max and yc2_min <=y<= yc2_max:
        counts['C2'] += 1

    elif xc3_min <= x <= xc3_max and yc3_min <=y<= yc3_max:
        counts['C3'] += 1

    elif xc4_min <= x <= xc4_max and yc4_min <=y<= yc4_max:
        counts['C4'] += 1

    elif xc5_min <= x <= xc5_max and yc5_min <=y<= yc5_max:
        counts['C5'] += 1

    elif xd3_min <= x <= xd3_max and yd3_min <=y<= yd3_max:
        counts['D3'] += 1

    elif xd4_min <= x <= xd4_max and yd4_min <=y<= yd4_max:
        counts['D4'] += 1

    elif xd5_min <= x <= xd5_max and yd5_min <=y<= yd5_max:
        counts['D5'] += 1

    else:
        counts['other']+= 1

        #print counts

    for i,j in coordinates:
        #print i,j
        compare(i,j)
    print counts

答案 1 :(得分:0)

刚刚找到this回答。命令行参数数组不是一个普通的数组,必须首先转换为实际的数组,如下所示:

list=("$@")