Reply to comment

Some Terse Perl Code to Manipulate an Array, Explained

Perl still rocks. People still say it's hard to read (true), but it's because the language is terse.

sub getAccountIdsOfSitesToSuspend
{
    $sql = <<EOQ;
        SELECT account_id
        FROM account
        WHERE
            account.account_balance < (SELECT triggerAmount FROM fk_suspension_rules)
            AND (
                    SELECT IF(SUM(transaction_ammount), SUM(transaction_ammount), 0)
                    FROM transaction
                    WHERE transaction_date > DATE_SUB( CURDATE(),
                        INTERVAL (SELECT paymentWindow FROM fk_suspension_rules) DAY)
                    AND transaction.account_id = account.account_id
                ) &lt; FLOOR( -account.account_balance * (SELECT releaseFraction FROM fk_suspension_rules))
            AND (
                    SELECT IF(COUNT(*), TRUE, FALSE )
                    FROM fk_domains
                    WHERE account_id = account.account_id
                    AND never_suspend<>'y'
                )
EOQ
    $sth = $dbh->prepare($sql);
    $sth->execute;
    my @result = ();
    map { push @result, $_->[0]; } @{ $sth->fetchall_arrayref([0]) };
    return @result;
}

This function runs an SQL statement, and then returns the result. It could be shorter, yes, but it has a nice way of collapsing a loop into a one-liner: map { push @result, $_->[0]; } @{ $sth->fetchall_arrayref([0]) };

map { ... } @ar -- this applies the code in the brackets to every element in array @ar.

@{ ... } -- dereferences the array reference returned in the brackets into an array.

fetchall_arrayref([0]) -- gets all the results from the query, slicing array element 0 from the results.

The syntax is ugly, but explicit (if you know perl).

Reply

The content of this field is kept private and will not be shown publicly.
  • Lines and paragraphs break automatically.

More information about formatting options

3 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.