in reply to Hashes, Arrays, Comparing Values to Elements

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

Replies are listed 'Best First'.
Re^2: Hashes, Arrays, Comparing Values to Elements
by Trihedralguy (Pilgrim) on Nov 30, 2008 at 15:16 UTC
    IG, Thanks for the quick response, i'll try and play around with your example sometime today. I plugged in in already today to see what it would do "out of the box" and it appears that perhaps increasing the efficiency of my perl code and color generator may not solve my 1 second delay between adding colors. (Really what I'm trying to do with this is cut down on the HTML it has to send back, which your script does a amazing job of doing, however, it seems jQuery (the javascript framework I'm using) understands what needs to be done, and re-assigns a background-color:(000,000,00)..(An RGB formatted bg color line added to each line.)...Which is MORE than the previous line.

    I'm not giving up on the perl side, I still think we can implement this way and have it much more efficient.

    Thanks again!