Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

tye has shown you one way. However, let me question the reasons for wanting what you are asking

Hashes are data structures where you can access elements by name rather than by position, as you do with arrays. Therefore a sorted hash is a contradiction. (Although Perl Tie mechanism can change something)

Now, I see two reasons for wanting the result from a query in a hash:

  • When you want to access your columns by name
  • When you want to process (e.g. display) your column names with their values.

In the first case, you don't really need a sorted hash, since you are using the hash names hardcoded in your application, so that the application itself will set the order. For example

while (my $rec = $sth->fetchrow_hashref()) { print $rec->{xy}, $rec->{xz}, $rec->{yz}; }

If you need to process your columns according to a list of column names, here is a slight variation on tye's answer:

while (my $row = $sth->fetchrow_hashref()) { print map( { "$_ => $row->{$_}\t"} @{$sth->{NAME}}), "\n"; } #or while (my $row = $sth->fetchrow_hashref()) { for (@{$sth->{NAME}}) { print "$_ => $row->{$_}\t"; } print "\n"; }

If you just want to use the column names together with their values, then you can use two different methods, according to your needs:

while (my $row = $sth->fetchrow_arrayref()) { # arrayref, not hash my $index = 0; print "$_ => ", $row->[$index++], "\t" for (@{$sth->{NAME}}); print "\n"; } # or the "C like" way while (my $row = $sth->fetchrow_arrayref()) { # arrayref, not hash for (my $index = 0; $index < $sth->{NUM_OF_FIELDS}; $index++) { print "$sth->{NAME}->[$index] => ", $row->[$index], "\t" } print "\n"; }

For more ideas, tips, and caveats on the same subject, see DBI Recipes.

If, as you say, you need an array of hash references, then it would be much easier this idiom:

my $aref = $sth->fetchall_arrayref({});

Notice the empty hashref passed as argument.

 _  _ _  _  
(_|| | |(_|><

In reply to Re: Ordering return from fetchrow_hashref by gmax
in thread Ordering return from fetchrow_hashref by slayer

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (5)
    As of 2020-01-17 14:23 GMT
    Find Nodes?
      Voting Booth?