Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Hashes, Arrays, Comparing Values to Elements

by Trihedralguy (Pilgrim)
on Nov 30, 2008 at 03:50 UTC ( [id://726871]=perlquestion: print w/replies, xml ) Need Help??

Trihedralguy has asked for the wisdom of the Perl Monks concerning the following question:

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;

Replies are listed 'Best First'.
Re: Hashes, Arrays, Comparing Values to Elements
by GrandFather (Saint) on Nov 30, 2008 at 04:20 UTC

    We don't have access to your database and are not likely to mock up your web site to test out the code. It makes it much easier for us (and for your own testing) if you mock up just enough context to test the code you are having trouble with. The following may point you in the right direction:

    use strict; use warnings; use strict; my @inColors = ( ['blue', 'foo'], ['red', 'bar'], ['blue', 'baz'], ); my %colors; for my $inColor (@inColors) { if (exists $colors{$inColor->[0]}) { print "Replacing $inColor->[0], $colors{$inColor->[0]} with $i +nColor->[1]\n"; } else { print "Adding $inColor->[0], $inColor->[1]\n"; } $colors{$inColor->[0]} = $inColor->[1]; }

    Prints:

    Adding blue, foo Adding red, bar Replacing blue, foo with baz

    Perl's payment curve coincides with its learning curve.
      Its all done, but this is a new revision that I want to figure out, hopefully to make the response time of the grid faster. Here is current what is running, the code for it is posted above. http://pixelated.hertzelle.com/draw.pl
Re: Hashes, Arrays, Comparing Values to Elements
by ig (Vicar) on Nov 30, 2008 at 05:46 UTC

    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.

    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
      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!
Re: Hashes, Arrays, Comparing Values to Elements
by Your Mother (Archbishop) on Nov 30, 2008 at 22:54 UTC
      What is XY?

      Also I'm not sure I need to use a module to generate the CSS, as its just something very simple... I haven't really had time to evaluate how to make this app preform better, both on the HTML side, the CSS side, the jQuery side and of course the perl side.

        An XY problem is where you ask for help with an implementation detail where in fact you are trying to solve the wrong problem. For example, people very often ask how to generate names for variables at run time ($x0, $x1, $x2, ...) when they should be using an array ($x[0], $x[1], $x[2], ...). The answer to the question they ask (the X question) is "use symbolic names", but the answer to the question they should have asked (the Y question) is "use an array".

        In one of your other replies there was a hint that you are doing this to speed things up. Most likely you should be describing the bigger problem and asking for help with speeding things up rather than focusing on this issue (that probably won't make the difference anyway).


        Perl's payment curve coincides with its learning curve.
        A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://726871]
Approved by McDarren
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-23 15:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found