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

Hi, My requirement is that when my script runs the command "show variables" all the variable name and values must be stored in an array or hash. Later i wish to pick up any variable name and it value and display it..can any body help me... The following code is listing all the variable name and there value at one stretch.
my $sgv = $dbh->prepare("show variables"); $sgv->execute(); while (($keyword, $value) = $sgv->fetchrow_array()) { print "$keyword=$value\n"; } I try to do the following... is this right? need help desperately..... my $sgv = $dbh->prepare("show variables"); $sgv->execute(); while (($keyword, $value) = $sgv->fetchrow_array()) { print $keyword, $value; %mysql{$keyword}=$value; print %mysql{"back_log"}; } This is the original output when the command runs in mysql. mysql> show variables; +---------------------------------+----------------------------------- +-------+ | Variable_name | Value + | +---------------------------------+----------------------------------- +-------+ | back_log | 50 + | | basedir | / + | | binlog_cache_size | 32768 + | | bulk_insert_buffer_size | 8388608 + | | character_set_client | latin1 + | | character_set_connection | latin1 + | | character_set_database | latin1 + | | character_set_results | latin1 + | | character_set_server | latin1 + |

Replies are listed 'Best First'.
Re: How to access hash value based on user defined condition.
by andreas1234567 (Vicar) on Nov 21, 2007 at 09:51 UTC
    You would benefit from prepending
    use strict; use warnings;
    to all your Perl code from now on. Always.

    Your code would probably work if you'd change it into something like:

    use strict; use warnings; # ... my %mysql = (); while (my ($keyword, $value) = $sth->fetchrow_array()) { $mysql{$keyword} = $value; } print $mysql{"back_log"};
    --
    Andreas
Re: How to access hash value based on user defined condition.
by olus (Curate) on Nov 21, 2007 at 13:11 UTC
    DBI does that for you.
    Consider doing:
    my $sgv = $dbh->prepare("show variables"); $sgv->execute(); my $href = $sgv->fetchall_hashref('Variable_name'); my $var_to_print = 'back_log'; print $$href{$var_to_print}{'Value'};

    --
    olus