This post is more about trying to find a cleaner way to accomplish my task. I use DBI a lot but I am curious if there is a better way to populate and array with the returned value when only a single field is returned. Example ---


SELECT `UserName` FROM Account a, asscAccountAccountGroup aaag, asscRoleAccountGroup arag, Role r WHERE a.Id = aaag.AccountId AND aaag.AccountGroupId = arag.AccountGroupId AND arag.RoleId = r.Id AND r.Name = 'ROLE_REVIEWER';

I have always acomplished this by doing the following --

my @regUsers; my $sth = $dbh->prepare("SELECT `UserName` FROM Account a, asscAccount +AccountGroup aaag, asscRoleAccountGroup arag, Role r WHERE a.Id = aaa +g.AccountId AND aaag.AccountGroupId = arag.AccountGroupId AND arag.Ro +leId = r.Id AND r.Name = 'ROLE_REVIEWER';") || die "Cannot prepare th +e query :$!"; $sth->execute() || die "Cannot execute the query :$!"; while ( my @userRows = $sth->fetchrow_array() ) { push @regUsers, @use +rRows; } print join("\n",@regUsers);
Which always works as expected but I'd like to simplify that code if possibe.

Doing the following seems to return a list of references but is close --

my @users = $dbh->selectall_array("SELECT `UserName` FROM Account a, a +sscAccountAccountGroup aaag, asscRoleAccountGroup arag, Role r WHERE +a.Id = aaag.AccountId AND aaag.AccountGroupId = arag.AccountGroupId A +ND arag.RoleId = r.Id AND r.Name = 'ROLE_REVIEWER'"); print join("\n",@users);

Thanks for the suggestions


In reply to Populating an array via a DBI call - simplified by edimusrex

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.