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

Hi all, I will try to ramble this off in an understandable pattern,,, I have a table in a database, whom has 3 columns. column 0 is primary key, so we are ignoring it for this question. The first is 'setting' the second is 'value'. This table can be empty or can have values. If it is empty, the perl script knows to use the default values. If one setting is missing ,it knows to replace it with its default value.

The simplest way I can think of for the perl code to handle the settings from the table, is something like this:
while (my @prev = $sql->fetchrow_array()){ $$prev[1]=$prev[2]; }

You probably notieced, this piece of code is not valid. But it shows exactly what I want, I want the value of $prev[1] to become a new string whose value is $prev[2]. IE, if $prev[1]=ip and $prev[2]=127.0.0.1 then after exiting the loop I would have: $ip=127.0.0.1

I know some other languages allow this exact piece of code, ie PHP, ect.

Can anyone help me do this in perl? It is the only language I will use for this.

Thank you all in advance, PyroX

Replies are listed 'Best First'.
Re: Dynamic Settings from Database, $$string help
by chromatic (Archbishop) on May 20, 2002 at 19:48 UTC
    Hashes exist for this purpose.
    my %settings; while ((undef, my ($name, $value)) = $sql->fetchrow_array()) { $settings{$name} = $value; }
    Update: This does mean you have to say $settings{ip} to get at the IP address. If you're familiar with named array values in PHP, it's very similar.
Re: Dynamic Settings from Database, $$string help
by broquaint (Abbot) on May 20, 2002 at 19:51 UTC
    Variable variables are bad, and what you're doing there is dereferencing an array reference, not symbolically dereferencing the value at index 1 of the $prev array as you may have thought. A much better way of dynamically storing the info would be to use a hash to store your data in e.g
    my %info = ( ip => '0.0.0.0'); my @list = (); while (my @prev = $sql->fetchrow_array()){ $info{ $prev[1] } = $prev[2]; push @list, { %info }; } # example usage of created list print $_->{ip}, "\n" for @list;
    Admittedly this is kinda overkill just to store a list of ip addresses, but it's scales to many keys quite easily. You may even want to use the fetchrow_hashref() for more clarity on what data you're using from the database. Also you'll probably also want to check out info on references, using strict and data structures in perl at the least on your journey to perl mastery.
    HTH

    _________
    broquaint

Re: Dynamic Settings from Database, $$string help
by PyroX (Pilgrim) on May 20, 2002 at 20:14 UTC
    Thanks for both you input, I appriciate the help.

    Neither of the solutions seem just right however.

    my %settings; while ((undef, my ($name, $value)) = $sql->fetchrow_array()) { $settings{$name} = $value; }

    I did:
    my %settings; while ((my ($tmp,$name, $value)) = $sql->fetchrow_array()) { $settings{$name}=$value; } $sql->finish(); print $lastlog."\n";

    Which returned nothing. lastlog is a 'name' and should be = 300000
    Did I do something wrong?

    Thanks broquaint, but I do not want to store a list. That is a very well written, however over-complicated for this task, bit of code.

    Remember, I am looking for the MOST EFFICIENT METHOD. I like any input however. This is more of a learning session for me, all input is valid. Thanks guys!
      Do you mean that $lastlog should have the value of the last $name? Then you can simply do:

      my (%settings,$name,$value); while ((undef,$name, $value) = sql->fetchrow_array()) { $settings{$name}=$value; } $sql->finish(); print "$name\n";
      -- Joost downtime n. The period during which a system is error-free and immune from user input.
      Change the final line of your code to
      print $settings{'lastlog'} . "\n";
      and you should see the results you expect.

      Yeah, it's a little more typing than what you sound like you're used to, but it's more than worth it. There is a technique in perl for using a variable as a variable name, but, as was mentioned earlier, it's bad, bad ju-ju. You don't want to get in the habit of going there, because as soon as you do, somebody's going to put the row 23, sql, MUHAHAHAH! into your database and clobber the query you're trying to read data from.