in reply to Unique Array from DB

Hashes are great for grouping.

my %users_by_account; ... push @{ $users_by_account{$account_name} }, $username; ... for my $account_name (sort keys %users_by_account) { my $users = $users_by_account{$account_name}; print("Account $account_name\n"); for my $username (@$users) { print("$username\n"); } print("\n"); }

Replies are listed 'Best First'.
Re^2: Unique Array from DB
by Corion (Patriarch) on Mar 09, 2009 at 16:13 UTC

    Why use a hash and sort yourself when the DB can do that already?

    select ... order by account_name, username

    then, in Perl:

    my $last_account; while (my $row = $sth->fetchrow_arrayref({ Slice => {})) { if (! defined $last_account or $last_account ne $row->{account_nam +e}) { if (defined $last_account) { print "\n"; }; $last_account = $row->{account_name}; print "Account $last_account\n", }; print $row->{username}; };
Re^2: Unique Array from DB
by Anonymous Monk on Mar 09, 2009 at 17:08 UTC
    If one more field from the DB would have to be added to this like including the date the account was opened,
    $date_opened = $pointer->{'dateopened'};
    Is that the case of one of those multidimensional hash?
    Printing,
    Group A - Name=Mary - Date Opened 03/03/2009