我如何将 (x,y) 坐标分配给某些值?

我如何将 (x,y) 坐标分配给某些值?

所以我基本上必须分配(x,y)协调来自某个文件的一些 ID。假设该文件items.txt包含以下内容:

if-eth0-in
if-eth0-out
ping-status
cpu-load

这些 ID 与一些将按顺序排列成表格的图表相关。表格如下:

          column1     column2
row1       (0,0)   |   (1,0)
row2       (0,1)   |   (1,1)
row3       (0,2)   |   (1,2)

当我将 ID 添加到表中时,我通过更改其X所以第一项是x=0&&y=0第二个x=1&&y=0

到目前为止,我一直在尝试使用for循环来实现这一点,但我面临的一个问题可能可以使用另一种语言轻松解决,例如perl或者Python基本上,我无法分配正确的值给就像这样:

item1: y=0
item2: y=0
item3: y=1
item4: y=1

等等,而X就像:

item1: x=0
item2: x=1
item3: x=0
item4: x=1

因此,用一个简单的语句来改变它是没有问题的if,但我不知道如何管理价值。

答案1

Perl:

#!/usr/bin/perl

my @coords = (
        [1, 1.1],       # coordinate 1, format x, y, first element has index 0
        [2, 2.2]        # coordinate 2
);
print "point 1: coord x=". $coords[0][0]."\n";
print "point 1: coord y=". $coords[0][1]."\n\n";

# change y for coord 2
$coords[1][1] = 10;

print "point 2: coord x=". $coords[1][0]."\n";
print "point 2: coord y=". $coords[1][1]."\n";

输出:

# ./test.pl
point 1: coord x=1
point 1: coord y=1.1

point 2: coord x=2
point 2: coord y=10

以供参考:Perl 中的多维数组

答案2

使用提供的其他答案可能也是可行的,但是,由于我正在寻找一些非常简单的东西,所以我做了这个,效果很完美:

#!/bin/bash

# First position will always be (1,0), let's set it.
x=1
y=0

# Variables that control wether we have to do a '+1' for $y
sum="no"
ftime="yes"

# For loop to check how the script would work.
# The final script reads from a file some ids instead but this helps testing
for i in {1..10};
do

# Do we have to do $y+1?
if [ $sum = "yes" ]; then
  let y=$y+1
fi

# Echo output
  echo "---------"
  echo "($x,$y)"

# Let's do the set up for the next run of the loop.
# x changes every time it runs so, easy 'if' condition.
if [ $x -eq 1 ]; then
  x=0
elif [ $x -eq 0 ]; then
  x=1
fi

# Check if it's the first time the loop is running.
# If not, then we have to change the value of sum.
# Otherwise, we don't change it so it goes as we want.
if [ $ftime = "no" ]; then
  if [ $sum = "yes" ]; then
    sum="no"
  elif [ $sum = "no" ]; then
    sum="yes"
  fi
elif [ $ftime = "yes" ]; then
  ftime="no"
fi

done

输出:

---------
(1,0)
---------
(0,0)
---------
(1,1)
---------
(0,1)
---------
(1,2)
---------
(0,2)
---------
(1,3)
---------
(0,3)
---------
(1,4)
---------
(0,4)

可能不是最好或最漂亮的解决方案,但希望它能对某些人有所帮助:)

相关内容