Hello monks!

I have a problem. First I've talked to a few monks in the chat tonight, and they explained a little, but I think there was a little confusion. I need help!

Basically what I have is a list of two elements in a database, like so:

Key = ElementID and Value = ColorCode

With these I want to build a "grid" of colors, using DIV's. However currently I'm using style="background-color:#$color_code" and I think that is just too much "stuff" to pack up and send to the user interface. So I want to simplify everything down into id=b$elementid (If that makes sense..)

However, this is the part I keep getting confused on...I want to check my list of elements to see if its already been used, and if it has, redefined it. So when I build my CSS file I will have something like this: .b1 {background-color: #fff...b2...b3...b4 and so on...However if one of the colors are the same...Dont use it, but redefined that element to use the other color. (Confusing I know!) Here is a sample of some code I've been working on, it doesnt check anything yet, because I cant figure out what I need to do? Please let me know if you see anything that pops out that would make this so much easier.

Thank you!
#!/usr/bin/perl use DBI; use CGI qw/:standard/; use strict; my $cgi = new CGI; print header('text/html'); my $color_sent = $cgi->param('colorselect'); my $id_select = $cgi->param('id_select'); if (!$color_sent && !$id_select) { exit; } $color_sent = substr($color_sent, 1); $id_select = substr($id_select, 1); my $dbh = DBI->connect("DBI:mysql:xxxxxxxxxxx:xxxxxxxxxxxx", "xxxxxxxxxxxxx","xxxxxxxxxxxxxxxxxx"); if ($id_select ne 'esult') { #Update the database. } my $get_all_colors = $dbh->prepare(" SELECT color_code, element_id FROM current_information ORDER BY element_id "); $get_all_colors->execute(); #thezip told me about this #if (exists($hash{$value}) { # ... do something ... } #jporter told me about this #$hash{$key} ||= { }; $hash{$key}{$slot} = 'foo'; my $colorchart{0} ||= { }; my $key = 0; while (my ($color_code, $element_id) = $get_all_colors->fetchrow_array +) { if (exists($hash{{$element_id}) { # ... do something ... } $colorchart{$element_id} = '$color_code'; $key++; } print qq^ <style type="text/css">^; my $counter = 0; while ($key >= $counter) { .b$colorchart{$key}{2} {color: #$colorchart{$key}{1} }; $counter++; } print qq^; </style>^; $counter = 0; while ($key >= $counter) { print qq^<div id="b$element_id" class="btn px"><a href="#"></a></d +iv>^; $counter++; } #this is what I use to have before I wanted to get smart. #while (my ($color_code, $element_id) = $get_all_colors->fetchrow_arra +y) #{ #print qq^<div id="b$element_id" class="btn px" style="background-colo +r:#$color_code"><a href="#"></a></div> #^; #} $dbh->disconnect(); exit;

In reply to 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.