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

Replies are listed 'Best First'.
Re: How can I specify which array to use with $ENV(queary_string)?
by chromatic (Archbishop) on Dec 15, 2001 at 01:06 UTC
    jaldhar has the right idea. What you're trying to do is to use symbolic references. Here's my taunt: don't do that.

    Here's a better approach:

    my %colorsets = ( samn => [ "#A5A5A5", "#84B594", "#84A5C6", "#DE6BA5" ], atari => [ "#00ff00", "#8fbc8f", "#ffcc00", "#000000", "#33cc00", " +#330099", "#363636" ], ); my $set = $ENV{QUERY_STRING}; my $colors = (defined $set and exists $colorsets{$set}) ? $colorsets{$set} : $colorsets{samn}; my $numcolors = @$colors; for (0 .. $totcells) { my $cellcol = $colors->[ rand $numcolors ]; # the rest as above }
Re: How can I specify which array to use with $ENV(queary_string)?
by jaldhar (Vicar) on Dec 10, 2001 at 16:41 UTC

    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;
      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.

130618
by Samn (Monk) on Dec 10, 2001 at 13:53 UTC