[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] An odd error message...



At 8:00 PM 7/20/00, A. R. Goldman wrote:
>Greetings:
>I am new to Perl, and fear that this may be a very elementary question,
>I hope that you'll bear with me...
>I have been working on the following code (the goal of which is
>ultimately to consult a database, and determine the number of items
>which need to be reordered):
>
>
>#!/usr/bin/perl -w
>use strict;
>
>my $dbase = 'stock.dat';
>my $report = 'restock.txt';
>my $order;
>
>open (DBASE, $dbase) || die "can't open $dbase: $!";
>open (REPORT, ">$report") || die "can't open $report: $!";
>
>
>while ( <DBASE> ) {
>     chomp;
>
>     my @fields = split;
>     next if $fields[3] < 15 and $fields[2] >= 50;
>     next if $fields[3] >= 15 and $fields[2] >=20;
>
>
>
>     if ($fields[2] < 50 and $fields[3] <= 15){
>         $order = "more of these";
>
>
>     }
>
>     print REPORT "$fields[0] : $fields[1]\n";
>     print REPORT "\torder: $order\n";
>
>
>}
>close DBASE;
>close REPORT;
>__END__
>
>with this Data Base:
>
>123 widget 3 17.00
>214 gadget 8 15.50
>313 thingamabob 27 0.2
>222 foo 9 2.00
>133 a_nut 11 25.00
>144 b_nut 29 22.00
>211 c_nut 55 5.00
>
>And I am getting variations on the following error message:
>
># Use of uninitialized value, <DBASE> chunk 1.
>File 'Macintosh HD:MacPerl ƒ:MacPerl Scripts:reorder_b.pl'; Line 28
># Use of uninitialized value, <DBASE> chunk 2.
>File 'Macintosh HD:MacPerl ƒ:MacPerl Scripts:reorder_b.pl'; Line 28
>
>I understand from a mentor that this works on other platforms;
>therefore, either I'm making a coding error that I can't see, or I'm
>coming up against something specific to MacPerl...

1. Not macPerl-specific. When you use the statement

    my $order;

you have declared the scope of the variable $order, but you haven't 
given it a defined value.

2. Another problem:
Once your innermost if clause gets a true value, nothing in this 
script will change $order back to no longer say "more of these", so 
from then on it the script will return "order: more of these". You 
can see this if you change the order of your data records. Put the 
line that starts 313 first, and your inventory will look quite low! 
Plus, you won't get an uninitialized value error, because $order will 
have a value every time the script gets to the print command.

Try this in there:

     if ($fields[2] < 50 and $fields[3] <= 15){
         $order = "more of these"
     } else {
     	$order = ''
     }

or similar logic.

Have to go to dinner, so this is a fast response, but HTH.

-- 

- Bruce

__Bruce_Van_Allen___bva@cruzio.com__Santa_Cruz_CA__

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org