Okay, I am working on a function that inserts a row into a MySQL table. Now, I am trying to make this thing slightly intelligent. So I have a hash that looks like the following:

my %TABLES = ( # Hash identifying one database 'users' => [ # Hash keys are the database's tables 'username', 'password', # Columns of the table in an array 'first_name', 'email_address', 'rating' ] );

Next I have a subroutine in my module that is called like the following:

$handle->insert ( -table => "users", -primary_key => "username", -values => { username => "mt2k", first_name => "Nathan", email_address => 'email@example.net', password => crypt("password", $salt), rating => 10 } );

Next comes the part I am having problems with. Here is a sample part that is close to that of my subroutine:

sub insert { my ($self, %q) = @_; # Set up placeholders my $ph = join ', ', ('?') x values %{$q{'-values'}}; # Prepare the query for the database my $sth = $self->{DB}->prepare( "INSERT INTO $q{'-table'} VALUES($ph)" ); # This is the line that causes problems. Read below for more. $sth->execute(values %{$q{'-values'}});

If you didn't catch on immediately, the problem lies in the fact that I am supplying the values of the hash to execute(). This means they are not in the correct order that MySQL requires them to be in. This is where the %TABLES hash comes in. This hash contains the arrays that hold the correct order in which the values must be passed as placeholders in the execute() statement.

So what I need to do is take the values of %{$q{'-values'}} and return them sorted in the correct order as identified by %TABLES.

I hope I've been clear enough in order to recieve some answers to this question which has perplexed me. Thanks in advance!


In reply to Sorting hash values according to order of an array by mt2k

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.