in reply to Dereferencing array of hashrefs

The expression (fetchrow_hashref($dbh, $query))[0] evaluates to a hashref. You can dereference a key from any hashref by using EXPR->{key}. You can also omit the arrow operator if EXPR ends with an array or hash index.

Putting that together gives:

print +(fetchrow_hashref($dbh, $query))[0]{key}, "\n";
You need the + to disambiguate the expression from print(). All in all, it's a little messy, and having the extra variable is a reasonable tradeoff for readability.

Update: ysth is dead on, you do need the arrow!

blokhead

Replies are listed 'Best First'.
Re: Re: Dereferencing array of hashrefs
by ysth (Canon) on May 13, 2004 at 02:37 UTC
    You can also omit the arrow operator if EXPR ends with an array or hash index.
    An array or hash index, yes, but not a list slice; here you do need the ->.
Re: Re: Dereferencing array of hashrefs
by Guildencrantz (Sexton) on May 13, 2004 at 02:13 UTC
    Thanks!
    I would like to note that your consolidated version isn't working. It keeps kicking the error:
    syntax error at ./script.pl line 43, near "){" Execution of ./script.pl aborted due to compilation errors.
    However the expanded EXPR->{key} version works fabulously. I do agree that it's a bit ambiguous, but I find it more fitting in this particular instance.
    Again, thanks.
    ~~Guildencrantz