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

Is there another way of writing this code below :

my @logged_events; push @logged_events, $_ while $_ = $sth_le->fetchrow_hashref();


My idea is that I want to put specific values into the array.

Replies are listed 'Best First'.
Re: Hashref for specific values
by klassa (Acolyte) on Mar 04, 2011 at 13:13 UTC
    Per previous reply, it's not clear what you want to put into @logged_events. If all you want is the results from the fetchrow_hashref() calls, then what you've got will work. I'd suggest clarity over fitting-it-on-one-line, though. And really, there's no point in using $_ here.
    my @logged_events;
    
    while (my $record = $sth_le->fetchrow_hashref())
    {
        push @logged_events, $record;
    }

    seems cleaner to me. If you want specific things from the hash ref, then you can do, e.g.:

    while (my $record = $sth_le->fetchrow_hashref())
    {
        push @logged_events, $record->{SOME_KEY};
    }
Re: Hashref for specific values
by locked_user sundialsvc4 (Abbot) on Mar 04, 2011 at 13:55 UTC

    I strongly agree that you probably do not want to try to write it “all on one line.”   Doing so makes no difference to the Perl compiler, but it makes the code considerably harder to maintain going forward.   When you meditate on, “how shall I write this?”, remember that the code may next be touched by a complete stranger (which might be you) who’s under pressure and in a hurry.   She will need to instantly ascertain what your code actually does, and then she will need to accurately write a source-code patch that will make the code do something new.   “Perl Golf” is fun to write, but not fun to encounter in the field.   Little things make a big difference.

      ...but... ...but... ...but... this is the stuff that makes Perl fun... ...and so elegant...

      ...might as well code in some braindead language (like Java), if idiomatic Perl confuses the noobs...

Re: Hashref for specific values
by ikegami (Patriarch) on Mar 04, 2011 at 17:27 UTC
    push @logged_events, [ @$_{qw( foo bar )} ] while $_ = $sth_le->fetchrow_hashref();
Re: Hashref for specific values
by Anonymous Monk on Mar 04, 2011 at 12:34 UTC
    Yes, it all depends on what you want @logged_events to look like, so what do you want @logged_events to look like?
Re: Hashref for specific values
by roboticus (Chancellor) on Mar 04, 2011 at 23:37 UTC

    hmadhl:

    So you're wanting to take only a portion of the columns? Why not just ask for the columns you want?

    my @t=DBI::selectall_arrayref("select col1, col2 from foo where bar=ba +z");

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Hashref for specific values
by hmadhi (Acolyte) on Mar 07, 2011 at 06:01 UTC
    Thank You for all the answers: This is the solution I have come up with.
    while ($rows_le = $sth_le->fetchrow_hashref()) { if ($rows_le->{'KPI'} eq 'PDP'&&$rows_le->{'SPECIFIC_PROBLEM'} eq 'PDP +') { push @logged_events , $rows_le; } }
    I know this could be done in the SQL, however for my specific application I need to do the sorting at a later stage.