Operating System - Linux
1748078 Members
5455 Online
108758 Solutions
New Discussion юеВ

Re: how to round robin a variable?

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

how to round robin a variable?

I have set an array of ten HTML hex colors to be assigned to a system when printing config data to a HTML document:

clcnt=0
echo $COLORS |while read INCOLORS
do
(( clcnt +=1 ))
OUTCOLOR[$clcnt]=$INCOLORS
echo "$clcnt \c"
echo ${OUTCOLOR[$clcnt]}
done

1 #CC9999
2 #9999FF
3 #FFCC88
4 #FF99FF
5 #FFFF66
6 #FF9933
7 #CC9966
8 #990099
9 #006666
10 #993300

I now need to associate each color with a system that resides in $SysList so when I output the data to HTML I create a table and each cell represents a different system.

How can I associate this data to each lpar in $SysList - I will use the color to represent a system more than once.

any help is much appreciated as always :-)

Thanks

Chris.
hello
9 REPLIES 9
Fabien GUTIERREZ
Frequent Advisor

Re: how to round robin a variable?

seems like you first must set an array with your hex codes then a loop into your systems using a modulo the number max of colors you decided to go through
Laurent Menase
Honored Contributor
Solution

Re: how to round robin a variable?

if $Syslist is a string
"sys1 sys2 sys3" for instance
typeset -i idxsys=0
typeset -i idxcol=0
for i in $Syslist
do
SYSNAME[$idxsys]=$i
SYSCOL[$idxsys]=${OUTCOLOR[$idxcol]}
idxsys=idxsys+1
idxcol=idxsys%${clcnt}
done

or you can
typeset -i idxsys=0
typeset -i idxcol=0
for i in $Syslist
do
eval "SYSCOL_${i}=${OUTCOLOR[$idxcol]}"
idxsys=idxsys+1
idxcol=idxsys%${clcnt}
done

Then you can use eval echo $"{SYSCOL_${i}}"
Pete Randall
Outstanding Contributor

Re: how to round robin a variable?

Chris,

We use the following snippet to randomly assign dbspaces at login time:

NUMBER=`date +%S | cut -c2,2`
set +A LINE_ARRAY `cat /etc/dbspace`
DBSPACETEMP=${LINE_ARRAY[$NUMBER]}
export DBSPACETEMP

where /etc/dbspace looks like this:

$ cat /etc/dbspace
dbs14 dbs13 dbs12 dbs11 dbs09 dbs10 dbs14 dbs13 dbs12

I think it would be simple to adapt this for your use.


Pete

Pete
James R. Ferguson
Acclaimed Contributor

Re: how to round robin a variable?

Hi Chris:

You could use 'awk' to generate a random index into your array if you like:

# awk 'BEGIN{srand();printf("%d\n",(1+int(rand()*10)))}'

Regards!

...JRF...

Hein van den Heuvel
Honored Contributor

Re: how to round robin a variable?

Did I call that or what?

See:

http://forums11.itrc.hp.com/service/forums/questionanswer.do?threadId=1238236

What problem are you really trying to solve?

Once you have an lpar - color association, then how/where will that be used?
What will be the 'driver'?
That $SysList or something else still?

$ perl -e '@sys=<>; @colors=split /\s+/, $ENV{COLORS}; for $i (1..@sys) { print "$i $colors[$i-1] $sys[$i-1]" }' $SysList
1 #CC9999 aap
2 #9999FF noot
3 #FFCC88 mies

fwiw,
Hein.



James R. Ferguson
Acclaimed Contributor

Re: how to round robin a variable?

Hi Chris:

Another way using Perl hashes (known in 'awk' as associative arrays):

# cat myref
#!/usr/bin/perl
use strict;
use warnings;
my $sys = shift or die "systemId expected\n";
my %colors = (
sys1 => '#CC9999',
sys2 => '#9999FF',
sys3 => '#FFCC88',
sys4 => '#FF99FF',
sys5 => '#FFFF66',
sys6 => '#FF9933',
sys7 => '#CC9966',
sys8 => '#990099',
sys9 => '#006666',
sys10 => '#993300'
);
if ( exists $colors{$sys} ) {
print "$colors{$sys}\n";
}
else {
print "#000000\n";
}

...run passing the "system" for which you want to find the color:

# ./myref sys3
#FFCC88

# ./myref badsys
#000000

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: how to round robin a variable?

Hi (again) Chris:

...and the last Perl solution hacked in 'awk':

# cat ./myref
#!/usr/bin/awk -f
END{
colors["sys1"]="#CC9999"
colors["sys2"]="#9999FF"
colors["sys3"]="#FFCC88"
colors["sys4"]="#FF99FF"
colors["sys5"]="#FFFF66"
colors["sys6"]="#FF9933"
colors["sys7"]="#CC9966"
colors["sys8"]="#990099"
colors["sys9"]="#006666"
colors["sys10"]="#993300"
if ($1 in colors)
print colors[$1]
else
print "#000000"
}

...run as:

# echo sys8 | ./myref
#990099

...oh, I so much prefer Perl :-)

Regards!

...JRF...
lawrenzo_1
Super Advisor

Re: how to round robin a variable?

NICE ...

I'll take a look at the exapmles and adapt for my purpose!

thanks again

Chris.
hello
lawrenzo_1
Super Advisor

Re: how to round robin a variable?

thanks all for the advise and help ...

the solution that I found worked best to my skills and uderstanding:

for LPAR in $LparList
do
LPARNAME[$idlpar]=$LPAR
LPARCOL[$idlpar]=${outcolor[$idxcol]}
idlpar=idlpar+1
idxcol=idlpar%${clcnt}

done
hello