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


From the code, Each row would contain variables for $idx and $team.


If 3 rows contains the following variables e.g.

$idx = 1; $team = team a; $idx = 2; $team = team a; $idx = 3; $team = team a;


How do I to get to display the 3 rows that contains all "team a" and the $idx from the while statement.

team a 1 2 3
#CODE STARTS HERE# sub initialize { local ($x) = @_; if ($x == 1) { $idx = &dummyvoid($row->{'idx'}); $team = &dummyvoid($row->{'team'}); } } while ($row = $sth->fetchrow_hashref) { &initialize(2); }

Replies are listed 'Best First'.
Re: Getting specific variable data per row.
by davorg (Chancellor) on Nov 27, 2001 at 15:47 UTC

    Your code is a little confusing (what is the point of initialize? What does dummyvoid do? Why is $row a global variable?) but I think this will do what you want.

    my $prev_team = ''; while (my $row = $sth->fetchrow_hashref) { if ($row->{team} eq $prev_team) { print "\t$row->{idx}\n"; } else { print "$row->{team}\t$row->{idx}\n"; } $prev_team = $row->{team}; }

    Update: Hofmator points out (correctly) that this only works if your SQL query sorts by team name.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you don't talk about Perl club."


      Here are sample data

      data: ------------------ $team $user $idx $entries A A1 1 1 A A1 2 1 A A1 3 1 A A2 4 1 B B1 5 1 ------------------

      I am trying to display something like this

      display: ---------------------------- TEAM USER ENTRIES ---------------------------- A A1 3 A2 1 ---------------------------- 4 - TOTAL ---------------------------- B B1 1 ---------------------------- 1 - TOTAL ----------------------------