Shell中的数组操作

时间:2018-12-28 17:45:21

标签: arrays linux shell

在Linux shell脚本中,我定义了一个数组和一个常量,并希望通过数学计算生成一个新数组,例如

ix=(100 200 300)
dh=2.5
y=$(dh*(ix-1))

但是我无法获得新的数组y,因为运行shell srcipt时出现语法错误。如何纠正它以获得结果?

1 个答案:

答案 0 :(得分:0)

您必须编写一个循环。要使用小数进行算术运算,您需要使用ix=(100 200 300) y=() dh=2.5 for i in "${ix[@]}" do y+=($(echo "$i * $dh" | bc)) done echo "${y[@]}" 之类的工具,大多数shell仅支持整数算术。

250.0 500.0 750.0

输出:

int numberOfPlayers = Int32.Parse(Console.ReadLine());
//List which will contain each Player object
//Why use a list? A List is container which holds a reference to each
//Player object which is created in the loop,
//Reference to the List object
//https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=netframework-4.7.2

List<Player> players = new List<Player>();

for(int i = 0; i <= numberOfPlayers; i++)
{
    //Just instantiate a new Player object, make sure to pass in an integer for the second parameter of your constructor
    Player player = new Player(new int[5, 3], i, "Player " + i);
    //Add the Player to the List
    players.Add(player);
}