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

Basically I'm trying to print the column headers from a db query. I can iterate through each row and using printf I can print out the data, but it doesn't work when I'm trying to print out the column headers.

eg:

@row = $sth->fetchrow_array(); printf "%15s %12s %12s %12s %9s %5s $5s", @row;
- produces a formated line of data from the database

But this same approach applied to the column headers prints only ARRAY(0x168218):

@fieldNames = $sth->{NAME_uc}; printf "%15s %12s %12s %22s %9s %5s $5s", @fieldNames;
It doesn't much seem to matter if I replace @fieldNames with $sth->{NAME_uc} or @$fieldNames, I still don't get the array interpreted as a list, despite the fact that this appears to be the same methodology used to display the row data.

What hoops am I supposed to be jumping through here? Iterating through the column name array is a poor option because then I'd have to manually format the whole thing which seems a huge waste of code.

Replies are listed 'Best First'.
Re: printing an array reference
by Tanktalus (Canon) on Jul 18, 2005 at 20:24 UTC
    @fieldNames = @{$sth->{NAME_uc}};

    See perlref for more information on this. Like in many other languages, you gotta tell the compiler whether you want the reference, or what it references - this is perl's syntax. Changing for perl6, I think.

      That appears to have done it. I have been going back and forth between the Camel, LLama, and the Camel Head looking for this syntax. The perl.doc website has been quite reluctant to come up from my location so I appreciate your patience and assistance.

      I have to say, I would not have picked up on this after a quick reading of the perlref page ( which deigned to come up this time ). Thanks for distilling the idea for me.

Re: printing an array reference
by cmeyer (Pilgrim) on Jul 18, 2005 at 20:26 UTC

    An arrayref is a scalar value, so you should store it in a scalar variable. There are several ways of dereferencing (turning it into an array) an arrayref. One of the easiest is to prepend the scalar variable that holds an arrayref with the array sigil ("@").

    my $fieldNames = $sth->{ NAME_uc }; printf "%15s %12s %12s %22s %9s %5s $5s", @$fieldNames;

    Take a read through some documentation, such as 'perldoc perlreftut', and the Q&A section here on Perlmonks (Here's a link to the references section).

    -Colin.

    WHITEPAGES.COM | INC

Re: printing an array reference
by davidrw (Prior) on Jul 18, 2005 at 20:27 UTC
    The "Statement Handle Attributes" section of the DBI docs (perldoc DBI) documents the NAME, NAME_lc, NAME_uc attributes as being read-only array-refs (whereas $sth->fetchrow_array is documented as returned an array of the values). So you need to assign these values to a scalar and treat it as an array ref:
    my $fieldNames = $sth->{NAME_UC}; print join ":", @$fieldNames;
Re: printing an array reference
by shiza (Hermit) on Jul 18, 2005 at 20:38 UTC
    You could also use Data::Dumper to print the contents of the array ref.
    print Dumper(@fieldNames);
Re: printing an array reference
by friedo (Prior) on Jul 18, 2005 at 20:24 UTC
    I'm not up on the specifics of how DBI handles field names, but let's assume each one is stored in the first element of the referenced array. You could get them all out with a map.

    @fieldNames = $sth->{NAME_uc}; printf "%15s %12s %12s %22s %9s %5s $5s", map { $_->[0] } @fieldNames;

    Update: I misunderstood the question -- I thought you had an array-of-arrays but it looks like just a simple array ref. Do what Tanktalus said. :)