Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

sorting return from

by Anonymous Monk
on Sep 02, 2005 at 21:20 UTC ( [id://488795]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

I make a request from a mysql table using the following function -

my $hashrow = $sth->fetchrow_hashref;

The data being returned should consist of x number of rows organised into 3 columns. The first column refers to a name, and the second two columns contain their age and height. I now wish to generate the following output from my request -

Name: Age, Height

John 34, 154
Michael 45, 176
Sinead 31, 159
James 23, 171
etc.

How do I do this using the returned hash reference? I'll need to look though the hash using a foreach statment fo starters.

Thanks.

Replies are listed 'Best First'.
Re: sorting return from
by Fang (Pilgrim) on Sep 02, 2005 at 21:41 UTC

    I believe you're looking for the fetchall_arrayref method. You can use it like this:

    my $rows = $sth->fetchall_arrayref; print "$_->{'name'} $_->{'age'}, $_->{'height'}" for (@$rows);

    If the number of returned rows is huge however, you may want to fetch them one at a time, using fetchrow_hashref:

    while (my $row = $sth->fetchrow_hashref) { print "$row->{'name'} $row->{'age'}, $row->{'height'}; }

    Update: confused fetchall_hashref with fetchall_arrayref...

      Try this-

      while ( $ref = $sth->fetchrow_hashref() ) { print "$$ref{'name'} \t $$ref{'age'} \t $$ref{'height'}\n"; }
Re: sorting return from
by bmann (Priest) on Sep 02, 2005 at 23:40 UTC
    If I read your question right (correct me if I'm wrong, it's a small data sample) you are asking how to sort the results by height, ascending.

    If so, it makes the most sense to do it in SQL, ie SELECT Name, Age, Height FROM table ORDER BY Height ASC. Then you could step through it row by row, regardless of the size of the dataset returned.

    If it must be sorted in perl, you'll need to either use one of the "fetchall*" methods or loop through all rows to store the full result set before sorting.

    HTH

Re: sorting return from
by InfiniteSilence (Curate) on Sep 02, 2005 at 21:35 UTC
    What's your question...
  • How to connect to the database?
  • How to submit a SQL statement to the database?
  • How to write a while loop to traverse over the elements of the hashref? (Check the perldoc for that)

    ???

    Celebrate Intellectual Diversity

      Sorry
      Title should be, "sorting return from fetchrow_hashref"
      OK, I'm connecting to the database fine and I am getting back the right data into the hash ref because I've checked it using print Dumper $hashrow;
      Now I need to know how to use the hash ref, to print out the data in a formatted sense using a foreach loop.
      thanks.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://488795]
Approved by Errto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-26 07:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found