> my %groups;
> while (my $hashref = $sth->fetchrow_hashref) {
>     push @{$groups{$hashref->{group_num}}}, $hashref;
> }
>
> then later...
>
> foreach my $group_number (keys %groups) {
>     delete $groups{$group_number} unless group_is_OK($groups{$group_number});
> }

I don't understand why you're calling group_is_OK each time..
you can send all your %group to a sub that checks the group and deletes it if not
good? You would have to do one fuction call instead of calling it for each group!

Or if group_is_Ok is a function used elsewhere where you don't want to delete the
not_OK groups, you could have group_is_OK return an array of the keys where groups
are not ok (or of the keys where gourps are OK).. and then the deleting part would
just delete each record with the key in the array returned by the group_is_ok
function. (humm I'm not very clear, hope you understand!)

my 0.02$

Etienne


Don Seiler wrote:

> When I pass in a hash to a function like below:
>
> group_is_OK($groups{$group_number});
>
> I'm confused as to how to handle that argument with in the function,
> knowing the there are multiple values for that key.
>
> I start out with this:
>
> sub group_is_OK
> {
>   my $hash = $_;
>
>   # <insert a lot of confused and hopeless code here>
>
> }
>
> I want to loop through the collection of records passed in and perform my
> calculations.  I've tried for and foreach loops, but probably don't know
> what kind of data structure I'm dealing with.
>
> the %groups hash is defined as below.
>
> Any hints?
>
> On Thu, 1 Nov 2001, David Marshall wrote:
>
> > I'll second the suggestion made a few messages back about storing your
> > results in some other data structure (as opposed to re-traversing the data
> > structure that DBI gives you).
> >
> > In your circumstances, I'd probably put the retrieved rows in a hash of
> > arrays (keyed by group number) before doing anything else.  Then I'd
> > examine each array of records in the group and delete it from the hash if
> > it didn't qualify.  Then the HoA that is left over can be traversed for
> > whatever the final output is.
> >
> > The code might look something like this:
> >
> > my %groups;
> > while (my $hashref = $sth->fetchrow_hashref) {
> >     push @{$groups{$hashref->{group_num}}}, $hashref;
> > }
> >
> > then later...
> >
> > foreach my $group_number (keys %groups) {
> >     delete $groups{$group_number} unless group_is_OK($groups{$group_number});
> > }
> >
> > then finally...
> >
> > spew_group($_) foreach values %groups;
> >
> > YMMV on exact implementation.  In similar implementations, I will often
> > have other stuff in the data structure beyond that which I got directly out
> > of the database.  It all depends.
> >
> > At 01:26 PM 11/1/01 -0600, Don Seiler wrote:
> > >Perhaps I'm missing it, then.
> > >
> > >basically my query is this:
> > >
> > >select cust_no, acct_type, acct_status, group_num
> > >from cust,acct
> > >where cust.cust_no=acct.cust_no
> > >order by group_num
> > >
> > >the values of acct_type and acct_status for all of the records in
> > >a group determine if I want that group or not.  I don't think I can make
> > >that determination until I've gone through the recordset though.
> > >
> > >--
> > >Don Seiler             [EMAIL PROTECTED]
> > >Database Administrator / Sr Software Engineer
> > >NSightTel Billing LLC    Phone:  920.617.7501
> > >1580 Mid Valley Drive    Fax:    920.617.7493
> > >De Pere, WI  54115       Cell:   920.606.3240
> > >Pager: [EMAIL PROTECTED] / 920.613.2000
> > >
> > >
> > >On Thu, 1 Nov 2001, Marcelo Guelfi wrote:
> > >
> > > >
> > > > Are you sure that you can't use the GROUP BY clause?
> > > >
> > > > Saludos,
> > > >                   Marcelo.
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >                     "Don Seiler"
> > > >                     <Don.Seiler@Ce       To:     Michael Peppler
> > > <[EMAIL PROTECTED]>
> > > >                     llcom.com>           cc:     Marcelo
> > > Guelfi/Uruguay/Contr/IBM@IBMUY, <[EMAIL PROTECTED]>
> > > >                                          Subject:     Re: Looping
> > > through recordset twice
> > > >                     01/11/2001
> > > >                     16:13
> > > >                     Please respond
> > > >                     to "Don
> > > >                     Seiler"
> > > >
> > > >
> > > >
> > > >
> > > >
> > > > Basically, when I get to a new group number.  The record set is ordered by
> > > > group number, so all records in a group are together.  As I'm looping
> > > > through records in a group, I do some evaluation and add values to
> > > > variables.  When I get to a new group number, I look at the values.  If
> > > > they meet my criteria I add the last group number to an array.
> > > >
> > > > Then when I'm done I planned to loop again through the record set and if
> > > > the group number matches one in the array I'd print it.
> > > >
> > > > This is probably horribly inefficient and I'm leaning towards saving the
> > > > records to a tmp array and if they qualify saving that to master array for
> > > > later printing.
> > > >
> > > > Don.
> > > >
> > > > On Thu, 1 Nov 2001, Michael Peppler wrote:
> > > >
> > > > > Don Seiler writes:
> > > > >  > Actually the nature of the problem is what stopped me from doing this.
> > > > >  >
> > > > >  > I won't know which records I want until I look at the group of them.
> > > > >  >
> > > > >  > Example:  I have a table of records.  There is a "groupnum" column.
> > > > Many
> > > > >  > records have the same "groupnum", i.e. they are in the same group.
> > > > I'm
> > > > >  > only interested in selecting the group as a whole.  I will only know
> > > > if I
> > > > >  > want this group based on examining all of the records for that group.
> > > > >
> > > > > Hmmm - what condition determins that a group is complete?
> > > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> >
> >

Reply via email to