I keep doing stuff like this when I have to get a whole bunch of fields from a database and I want to not use hashref since I know array ref is faster.

Basically I make a select query and then get the array back and then dump each element from the array into the variable I need.

I thought about making an array of the fields and looping though the result array, but there probably is some magic answer I can't figure out.

thanks in advance

my $sqlQuery = "SELECT Object_ID, ID, Script_Name, Sql_Name, Min_Length, Max_Length, Char_Regex, Char_Allowed, Key_Test_Type, Key_Test_Table, Key_Test_Field, Update_Allowed, Display_Transform, Update_Transform, FieldType, FieldDataSource, Display_Order FROM OM_ObjectMetaData WHERE OM_ObjectMetaData.Object_ID = ?"; #print "<!-- $sqlQuery -->\n\n\n\n\n"; my $query = $dbh->prepare( $sqlQuery ); $query->execute( $self->{ID} ) || die $dbh->errstr; while( @result_array = $query->fetchrow_array ) { $self->{fields}{$result_array[2]}{Object_ID} = $result +_array[0]; $self->{fields}{$result_array[2]}{ID} = $result +_array[1]; $self->{fields}{$result_array[2]}{Script_Name} = $result +_array[2]; $self->{fields}{$result_array[2]}{Sql_Name} = $result +_array[3]; $self->{fields}{$result_array[2]}{Min_Length} = $re +sult_array[4]; $self->{fields}{$result_array[2]}{Max_Length} = $res +ult_array[5]; $self->{fields}{$result_array[2]}{Char_Regex} = $res +ult_array[6]; $self->{fields}{$result_array[2]}{Char_Allowed} = $resul +t_array[7]; $self->{fields}{$result_array[2]}{Key_Test_Type} = $res +ult_array[8]; $self->{fields}{$result_array[2]}{Key_Test_Table} = $result +_array[9]; $self->{fields}{$result_array[2]}{Key_Test_Field} = $result +_array[10]; $self->{fields}{$result_array[2]}{Update_Allowed} = $result +_array[11]; $self->{fields}{$result_array[2]}{Display_Transform} = $result +_array[12]; $self->{fields}{$result_array[2]}{Update_Transform} = $result +_array[13]; $self->{fields}{$result_array[2]}{FieldType} = $result +_array[14]; $self->{fields}{$result_array[2]}{FieldDataSource} = $result +_array[15]; $self->{fields}{$result_array[2]}{DisplayOrder} = $result +_array[16]; $self->{fields}{$result_array[2]}{Value} = ""; $self->{display_order}{$result_array[16]} = $result_array[2]; }

In reply to Better way of accessing fields by Angel

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.