Maybe the following will help. Note that I changed the CSS to use # instead of ., so that it is keyed to the ID instead of the class of the elements.

#!/usr/bin/perl use strict; use warnings; use CGI qw/:standard/; my %colorchart; while( my ($color_code, $element_id) = split(/\s+/, <DATA>)) { $colorchart{$element_id} = $color_code; } my $style; foreach my $key (sort keys %colorchart) { $style .= "#b$key {background-color: $colorchart{$key}}\n"; } print header, start_html(-title=>'Colors', -style => {-code => $style} +); foreach my $key (sort keys %colorchart) { print qq^<div id="b$key" class="btn px"><a href="#"></a>$key - $co +lorchart{$key}</div>\n^; } print end_html; __DATA__ #0000FF 1 Blue #8A2BE2 2 BlueViolet #A52A2A 3 Brown #DEB887 4 BurlyWood #5F9EA0 5 CadetBlue #7FFF00 6 Chartreuse

Update: Seeing your web page maybe helps me understand what you mean by However if one of the colors are the same...Dont use it, but redefined that element to use the other color. So, maybe the following is more what you are looking for:

#!/usr/bin/perl use strict; use warnings; use CGI qw/:standard/; my %seen; my $style; my $grid; while( my ($color_code, $element_id) = split(/\s+/, <DATA>)) { unless($seen{$color_code}) { $style .= ".c$color_code { background-color: #$color_code; }\n +"; $seen{$color_code} = $color_code; } $grid .= qq^<div id="$element_id" class="btn px c$color_code"><a h +ref="#"></a>$element_id - $color_code</div>\n^; } print header, start_html(-title=>'Colors', -style => {-code => $style} +), $grid, end_html; __DATA__ 0000FF 1 Blue 8A2BE2 2 BlueViolet A52A2A 3 Brown DEB887 4 BurlyWood 5F9EA0 5 CadetBlue 7FFF00 6 Chartreuse 0000FF 7 Blue 8A2BE2 8 BlueViolet

In reply to Re: Hashes, Arrays, Comparing Values to Elements by ig
in thread Hashes, Arrays, Comparing Values to Elements by Trihedralguy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.