2

So I basically have to assign (x,y) coordenates to some ids that will be coming from a file. Let's say the file items.txt have the following content:

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

Those ids are related to some graphs that will be ordered into a table. The table goes like:

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

When I'm adding the ids to the table, I assign them their cordenates by changing the value of their x and y so first item would have x=0 && y=0 and the second one x=1 && y=0

So far I've been trying to do it by using a for loop but I'm facing a problem that may have an easy solution using another language such as perl or python. Basically, I haven't been able to assign the correct value to y as it goes like:

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

and so on, while x goes like:

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

so there's no problem on changing that with a simple if statement but I have no clue on how could I do it to manage the y value.

1

2 Answers 2

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";

output:

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

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

For reference: Multi dimensional arrays in Perl

1

It was probably doable with the other answers provided but, as I was looking for something pretty simple, I did this, which is working perfect:

#!/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

Output:

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

Might not be the best or most beautiful solution but hope it helps someone :)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .