The breakout of where a subroutine ends and begins is largely a matter of 
personal preference.  You'll have to iterate over all the groups somewhere, 
I would tend to do it where my rough example showed so that subroutine 
group_is_OK wouldn't need to know whether the group being examined is part 
of a hash, array, or something else.  One could easily write another 
subroutine to do the hash traversal. YMMV.

For Don:

%groups is a hash of arrays, so the values of the hash are array 
references.  The elements of the array are themselves hashes, seeing as my 
example used fetchrow_hashref.

group_is_OK should begin like this, to continue my example.

sub group_is_OK {
    my $aryref = shift; # ref($aryref) will be eq 'ARRAY'
    foreach my $group_member (@$aryref) {
       # ref($group_member) eq 'HASH'
    }
    # some value here that reflects whether the group is OK
}

I apologize for having presented an incorrect amount of information 
(whether too little or too much, I don't know, heh).  I was mainly trying 
to illustrate why I think there are better alternatives to finding some way 
to loop through a recordset twice.


At 11:24 AM 11/2/01 -0500, Etienne Marcotte wrote:
> > 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