Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: How can I specify which array to use with $ENV(queary_string)?

by jaldhar (Vicar)
on Dec 10, 2001 at 16:41 UTC ( [id://130643]=note: print w/replies, xml ) Need Help??


in reply to [untitled node, ID 130617]

I couldn't get throught to your site but check this out:

#!/usr/bin/perl -Tw use strict; use CGI qw(:standard -no_xhtml -newstyle_urls); # (C) 2001, Jaldhar Vyas # Licensed under the Crowley Public License ('Do what thou wilt # shall be the whole of the license.') # Declare our color sets as a hash of references to arrays. my %colorsets = ( patriotic => ['red', 'white', 'blue'], sensitive => ['aqua', 'silver', 'teal'], fugly => ['maroon', 'fuchsia', 'lime'], ); # pick the color set from the query string, making sure it actually # exists in case someone sent us random crap. Default is # 'patriotic'. Use the trinary operator because it is so cool. my $colors =(param('scheme') && exists($colorsets{param('scheme')})) ? $colorsets{param('scheme')} : $colorsets{patriotic}; # print the page. print header, start_html(-title => 'color sets', -author => 'jaldhar@braincells.com'), p('The current scheme is ', param('scheme') || 'patriotic', '.'), table({border => 2}, Tr( # for each cell we pick a random color from our chosen color set. # Remember, $colors is a reference to an array so dereference # accordingly. This ought to be a subroutine to avoid repetition. td({bgcolor => @$colors[rand scalar @$colors], width => 100}, ' '), td({bgcolor => @$colors[rand scalar @$colors], width => 100}, ' '), td({bgcolor => @$colors[rand scalar @$colors], width => 100}, ' '), ), ), p('Select a color scheme:'), # A form to pick color sets from. start_form, radio_group(-name => 'scheme', -values => ['patriotic', 'sensitive', 'fugly'], ), br, submit, end_form end_html;
  • Comment on Re: How can I specify which array to use with $ENV(queary_string)?
  • Download Code

Replies are listed 'Best First'.
Re: Answer: How can I specify which array to use with $ENV(queary_string)?
by Anonymous Monk on Dec 10, 2001 at 18:55 UTC
    Thanks - My site is definately down so I won't get to test this until the dorks who host me get their act together. This doesn't really answer my question, but it looks like a viable alternative. I bounced my questions off my MIT pal, stumped him something else. Surely something so simple can be done with just a line of code! At any rate, gracias

      Whoops! It won't be the first time I misunderstood a question. :-) Though my answer is arguably the 'right' way to do it IMO. You don't want to deal with raw environment variables in a CGI script.

      Ok, how about this?

      $tdcolor = $ENV{'QUERY_STRING'}[rand scalar @ENV{'QUERY_STRING'}];

      The problem is you are feeding rand an array but it really wants a scalar. The scalar function gives you that with the added bonus that if you use it on an array, it return the number of elements in the array. So the net effect is you get a random element from the array. You will notice I used this technique in my earlier answer.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-19 20:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found