how to round robin a variable? (233 Views)
Reply
Super Advisor
lawrenzo_1
Posts: 560
Registered: ‎06-06-2003
Message 1 of 10 (233 Views)
Accepted Solution

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
Frequent Advisor
Fabien GUTIERREZ
Posts: 65
Registered: ‎01-23-2006
Message 2 of 10 (233 Views)

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
Honored Contributor
Laurent Menase
Posts: 1,079
Registered: ‎11-06-2003
Message 3 of 10 (233 Views)

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}}"
Outstanding Contributor
Pete Randall
Posts: 16,205
Registered: ‎11-03-1996
Message 4 of 10 (233 Views)

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
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 10 (233 Views)

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...

Honored Contributor
Hein van den Heuvel
Posts: 6,586
Registered: ‎05-19-2003
Message 6 of 10 (233 Views)

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.



Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 7 of 10 (233 Views)

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...
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 8 of 10 (233 Views)

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...
Super Advisor
lawrenzo_1
Posts: 560
Registered: ‎06-06-2003
Message 9 of 10 (233 Views)

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
Super Advisor
lawrenzo_1
Posts: 560
Registered: ‎06-06-2003
Message 10 of 10 (233 Views)

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
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.