----- Original Message ----- 
From: <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, October 06, 2004 4:32 AM
Subject: newbie stuck in a HOA


> Hi all,
>
> I have a flatfile database (falldata.tmp) that contains information about
> all the courses offered
> this includes: course number, course title, start date, day of week,
> location, html link, section number, fee1, fee2, and status.
>
> I have another file (cancel.txt) which contains a list of the courses that
> are canceled.

Not sure what's inside cancel.txt, course code or what ?

>
> What I want to do is change the status to "C" in the flatfile database, if
> it is in the cancel file.
>
> The problem I'm having is that
> I'm getting duplicate results and also incorrect results as the code isn't
> distinguishing between a canceled class and one that's not.
>
>
> Any ideas on where I'm going wrong.
> Thanks in advance,
> Pam
>
>
> sample data:
> 028076|Beginning Acting I|Sept 18|Sat|San Francisco|028086.html|1|275|0||
>
>
> results:
> OKAY 028860 ------------------
> 0: Impressionism: The Poetry of the Passing Moment
> 1: Oct 6
> 2: Wed
> 3: San Francisco
> 4: 028860.html
> 5:
> 6: 395
> 7: 0
> 8:
>
>
> OKAY 028860 ------------------
> 0: Impressionism: The Poetry of the Passing Moment
> 1: Oct 6
> 2: Wed
> 3: San Francisco
> 4: 028860.html
> 5:
> 6: 395
> 7: 0
> 8:
>
> CANCEL 028837 -----------------
> 0: Art, Architecture, and Culture of Cuba
> 1: Sept 13
> 2: Mon
> 3: Berkeley
> 4: 028837.html
> 5:
> 6: 365
> 7: 0
> 8: C
>
>
> OKAY 028837 ------------------
> 0: Art, Architecture, and Culture of Cuba
> 1: Sept 13
> 2: Mon
> 3: Berkeley
> 4: 028837.html
> 5:
> 6: 365
> 7: 0
> 8: C
>
>
>
>
> here's the code:
> #!/usr/bin/perl -w
> #Purpose update catalog database with C for cancel
> use strict;
>
> #make a backup
> #system `cp /dept/unxmkt/www/cgi-bin/catalog/fall.txt ./falldata.bak`;
> #system `cp /dept/unxmkt/www/cgi-bin/catalog/fall.txt ./falldata.tmp`;
>
> #declare global variables
> my $aref= "";

You won't need this . So please delete this.

>
> get_cancel();
> add_cancel($aref);

Simply instead with :
add_cancel ( get_cancel ) ;

>
> #open file of canceled courses, push it on array @edp_cancel
> sub get_cancel{
> my (@edp_cancel, $edp_cancel, $item, $status);

If not declare these vars for picking up arguments, don't declare
vars too early. It just no different for you to turn off strict and
warnings.

my @edp_cancel ; # is enough

> open(CANCEL, "cancel.txt") or die("no cancelfile: ");
>
> while( defined($_= <CANCEL>) ){ chomp($_);

Make it clear :
while (<CANCEL>) { chomp ;

> ($edp_cancel, $status) = split(/\|/, $_);

my ($edp_cancel, $status) = split /\|/, $_;

> push @edp_cancel, $edp_cancel;
> }
>
> #use array reference in order to return a value
> $aref = [EMAIL PROTECTED];
> return $aref;

return @edp_cancel ; # is enough
I don't see anywhere you need this to be an array reference.


> close CANCEL;

You should put this line before you 'returns'.

> } #end get_cancel
>
>
> sub add_cancel{
> #opens up copy of database, and splits data
> #if match on edp adds "C" to database in status field

my @cancel_list = @_;

We will use it later

>
> my (%course_info, $course_info, $item, @edp, $edp, $course, $start, $day,
> $loc, $link, $sec, $fee1, $fee2, $status, $cancel_edp, $i);

@edp, and then $edp, even that's ok, but it still likely to make
confusion... and
some evils... I guess :-)

my %course_info ; # is enough

>
> #DEBUG
> #for my $cancel_edp(@{$aref}){
> #print("$cancel_edp ");
> #}
>
> open(TEMP, "falldata.tmp") or die("no temp file: ");
>
> while( defined($_ = <TEMP>) ){

while (<TEMP>) { chomp ;

> #split up the fields
> ($edp, $course, $start, $day, $loc, $link, $sec, $fee1, $fee2, $status) =
split(/\|/, $_);

You won't need the above line, instead :

my @stuffs = split /\|/, $_;
my $edp = shift @stuffs;

>
> #put course info into hash of array with edp the key
> $course_info{$edp} = [$course, $start, $day, $loc, $link, $sec, $fee1,
> $fee2, $status];

$course_info{$edp}= [EMAIL PROTECTED] ;

>
> } #end while
>
> #DEBUG
> #foreach $item ( keys %course_info) {
> #print("$item: @{ $course_info{$item} }\n");
> #}
>
>
> #loop through all the courses in db
> #if edp matches a canceled edp, change status to C (cancel)
> foreach $item (keys %course_info){

foreach my $item ( keys %course_info ) {

> foreach $cancel_edp( @{$aref} ){

foreach my $cancel_item ( @cancel_list ) {
# I declare @cancel_list at this sub begins.

> if($item == $cancel_edp){

if ( $item == $cancel_item ) {

> #change the status to canceled
> $course_info{$item}[8] = "C";
>
> print("\n\nCANCEL $item --------------\n");

You print once here for a cancel item.
( in the foreach loop of @cancel_list )

> for $i (0 .. $#{ $course_info{$item} } ){

for my $i ( 0.. $#{$course_info{$item}} ){

> print("$i: $course_info{$item}[$i]\n");
> }
>
> }
>
>
> else{
> print("\n\nOKAY $item ----------------\n");
> for $i (0 .. $#{ $course_info{$item} } ){

for my $i ( 0.. $#{$course_info{$item}} ){

> print("$i: $course_info{$item}[$i]\n");

You print once here for a OK item
( still in the foreach loop of @cancel_list )


> }
>
> }
> } #end foreach of the canceled courses

The point you print twice is because this for loop is under @cancel_list,
each elems rolls a chance to print once. If there are 3 elems in the
@cancel_list,
you will print 3 times. The more elems in @cancel_list, more times you will
print.

So why the last round is OKAY while you can see that's "C" ? That's because
the
$item is no more targeting on 028837, so, that's the "ELSE" case, so, prints
OKAY.

> } #end foreach of the courses in the db
>
> close TEMP;
> } #end add_cancel
>

HTH,
Bee




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to