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

Bug#2254: perl doesn't initialize variable to 0 or something



Package: perl
Version: 5.002-1

In the following perl script, I must remove the comment "#" in line 14 in
order to get correct results.  This code worked in perl4 and should work
in perl5 because variables "should" be automagically initialized to 0, no?
Nonetheless the DEBUG output indicates that perl5 loses the first line of
pricing data (American Cheese) unless the comment in line 14 is removed.

The erroreous error can be seen:
$ coop.pl.prob | head -5
Reading in pricing data...
2.15 2.19 1.97 1.97 2.17 2.12 2.09 2.09 2.14 2.07 2.14 2.14 2.09 2.09 2.09 1.95 1.96
1.79 1.84 1.84 1.80 1.80 1.89 1.79 1.84 1.84 1.79 1.87 1.87 1.84 1.89 2.05 2.05 2.05
2.18 2.23 2.23 2.23 2.23 2.23 2.25 2.25 2.25 2.25 2.29 2.29 2.29 2.10 2.00 2.18 2.18

Here is the correct data:
$ coop.pl.prob | head -5
Reading in pricing data...
1.83 1.83 1.83 1.83 1.83 1.89 1.79 1.83 1.86 1.80 1.87 1.87 1.87 1.61 1.65 1.70 1.74
2.15 2.19 1.97 1.97 2.17 2.12 2.09 2.09 2.14 2.07 2.14 2.14 2.09 2.09 2.09 1.95 1.96
1.79 1.84 1.84 1.80 1.80 1.89 1.79 1.84 1.84 1.79 1.87 1.87 1.84 1.89 2.05 2.05 2.05
2.18 2.23 2.23 2.23 2.23 2.23 2.25 2.25 2.25 2.25 2.29 2.29 2.29 2.10 2.00 2.18 2.18

$cat -n coop.pl.prob
     1	#!/usr/bin/perl
     2
     3	if (scalar(@ARGV) == 0) { $ord_no = 0; }
     4	elsif ((scalar(@ARGV) > 1) || ($ARGV[0] eq "-h") || ($ARGV[0] eq "--help")) {
     5	  die "  coop.pl:  coop.pl [-hnH] where n is the date code for order number (1=061893)\n"; }
     6	elsif ($ARGV[0] eq "-H") { $horizontal = 1; }
     7	else { $ord_no = $ARGV[0]; if ($ord_no < 0) { $ord_no *= -1; } }
     8	$ord_no = $ord_no - 1; # Make ord_no begin w/ 0 (for arrays) or -1 for reports
     9	# Process "pricing" file; build: date_array, price_array, and item_array
    10	open(PRICING, "pricing") || die "  coop.pl: Couldn't open pricing: $!\n";
    11	if ($ord_no == -1) {
    12	  print "Reading in pricing data...\n";
    13	}
    14	$skip_first = 0; # $itemind = 0;
    15	while ($line = <PRICING>) {
    16	  chop($line); ($first_word, $remainder) = split(/\s+[\d\$]/, $line, 2);
    17	  $remainder =~ s/\s+\$/ /g;
    18	  if (($remainder) && ($first_word eq "Item")) {
    19	    if ($skip_first == 1) { die "This can't happen.\n"; }
    20	    $skip_first = 1;
    21	    $remainder = "0" . $remainder;
    22	    @date_array = ("061893", split(/\s/, $remainder)); }
    23	  elsif ($remainder) {
    24	    $item_array[$itemind] = $first_word;
    25	    @prices = split(/\s/, $remainder);
    26	    for ($dateind = 1; $dateind <= $#date_array; $dateind++ ) {
    27	      $price_array{$itemind, $dateind} = $prices[$dateind - 1]; }
    28	    $itemind++; }
    29	  else { ; # Discard the line
    30	  } }
    31	close PRICING;
    32	$DEBUG = 1;
    33	if ($DEBUG) {
    34	  for ($ind=0;$ind<$itemind;$ind++) {
    35	    for ($ind2=1;$ind2<$dateind;$ind2++) {
    36	      print "$price_array{$ind, $ind2} ";
    37	    }
    38	    print "\n";
    39	  }
    40	}
$cat pricing
Prices:
Food
Item             080693 101093 120593 011694 030694 050694 071594 091694 111194 011395 022095 040795 060495 072895 092295 102995 121595
American Cheese   $1.83  $1.83  $1.83  $1.83  $1.83  $1.89  $1.79  $1.83  $1.86  $1.80  $1.87  $1.87  $1.87  $1.61  $1.65  $1.70  $1.74
Provalone Cheese  $2.15  $2.19  $1.97  $1.97  $2.17  $2.12  $2.09  $2.09  $2.14  $2.07  $2.14  $2.14  $2.09  $2.09  $2.09  $1.95  $1.96
Muenster Cheese   $1.79  $1.84  $1.84  $1.80  $1.80  $1.89  $1.79  $1.84  $1.84  $1.79  $1.87  $1.87  $1.84  $1.89  $2.05  $2.05  $2.05
Swiss Cheese      $2.18  $2.23  $2.23  $2.23  $2.23  $2.23  $2.25  $2.25  $2.25  $2.25  $2.29  $2.29  $2.29  $2.10  $2.00  $2.18  $2.18
Mozzarella Ch.    $1.69  $1.77  $1.77  $1.72  $1.72  $1.82  $1.65  $1.69  $1.69  $1.63  $1.69  $1.69  $1.65  $1.73  $1.80  $1.89  $1.89
Sharp Cooper      $2.83  $2.89  $2.89  $2.84  $2.84  $2.84  $2.84  $2.84  $2.84  $2.84  $2.84  $2.84  $2.84  $2.45  $2.45  $2.49  $2.45
Cream Cheese      $1.25  $1.25  $1.25  $1.25  $1.25  $1.25  $1.25  $1.25  $1.25  $1.25  $1.29  $1.29  $1.29  $1.15  $1.15  $1.18  $1.22
Parmasan Ungrated $3.30  $3.30  $3.30  $3.30  $3.30  $3.30  $3.30  $3.30  $3.30  $3.30  $3.30  $3.30  $3.30  $3.30  $3.19  $3.19  $2.98
Romano Grated     $2.79  $2.79  $2.89  $2.89  $2.89  $2.89  $2.89  $2.89  $2.89  $2.89  $2.89  $2.89  $2.89  $2.95  $2.95  $2.95  $2.95
Eggs              $0.75  $0.75  $0.78  $0.85  $0.79  $0.79  $0.74  $0.74  $0.74  $0.74  $0.74  $0.74  $0.74  $0.89  $0.86  $0.85  $0.98
Margarine         $0.78  $0.78  $0.78  $0.78  $0.78  $0.78  $0.75  $1.09  $1.09  $1.09  $1.14  $1.14  $1.14  $1.14  $0.44  $0.49  $0.49
Almonds           $2.15  $2.15  $2.15  $2.15  $3.50  $3.50  $4.25  $4.25  $4.25  $4.25  $4.25  $4.25  $4.25  $4.25  $6.00  $6.00  $6.00
Pecans(halves)    $4.60  $5.50  $4.60  $4.60  $3.95  $3.95  $3.95  $3.95  $3.95  $3.95  $3.95  $3.70  $3.70  $3.70  $3.70  $6.80  $7.00
Cashew(pieces)    $3.00  $3.00  $3.00  $3.00  $3.60  $3.60  $3.75  $3.75  $3.75  $3.75  $3.75  $3.75  $3.75  $3.75  $3.75  $3.75  $4.95
Roast Peanuts     $1.50  $1.50  $1.50  $1.50  $1.95  $1.95  $1.95  $1.95  $1.95  $1.95  $1.95  $1.95  $1.95  $1.95  $1.95  $1.95  $1.95
Peanuts in shell  $1.25  $1.25  $1.25  $1.25  $1.25  $1.25  $1.25  $1.25  $1.25  $1.25  $0.85  $0.85  $0.85  $0.85  $0.85  $0.85  $0.85
Walnut(pieces)    $3.95  $2.50  $2.50  $2.50  $2.80  $3.95  $3.95  $3.95  $3.95  $3.95  $3.95  $3.95  $3.95  $3.95  $3.95  $3.95  $3.95
Papaya            $1.25  $1.25  $1.25  $1.25  $1.50  $1.50  $1.50  $1.50  $1.50  $1.50  $1.50  $1.50  $1.50  $1.50  $1.50  $1.50  $1.50
Apricots          $2.07  $2.07  $2.07  $2.07  $2.70  $2.70  $2.30  $2.30  $2.30  $2.30  $2.30  $2.30  $2.30  $2.30  $2.30  $2.30  $2.30
Pitted Dates      $2.07  $2.07  $2.07  $2.07  $2.65  $2.65  $2.65  $2.65  $2.65  $2.65  $2.65  $2.65  $2.65  $2.65  $2.65  $2.65  $2.65
Prunes            $1.75  $1.75  $1.75  $1.75  $2.15  $2.15  $2.50  $2.50  $2.50  $2.50  $2.50  $2.50  $2.50  $2.50  $2.50  $2.50  $2.50
Raisins           $1.25  $1.25  $1.25  $1.25  $1.25  $1.25  $1.25  $1.25  $1.25  $1.25  $1.25  $1.25  $1.25  $1.25  $1.25  $1.25  $1.25
Caligornia Figs   $3.95  $3.95  $3.95  $3.95  $3.95  $3.95  $3.95  $4.25  $4.25  $2.25  $4.25  $4.25  $2.25  $2.25  $2.25  $2.25  $2.25
Coconut-sugar     $1.25  $1.25  $1.25  $1.25  $1.25  $1.25  $1.50  $1.50  $1.50  $1.50  $1.50  $1.50  $1.50  $1.50  $1.50  $1.50  $1.50
Coconut-no sugar  $1.25  $1.25  $1.25  $1.25  $1.25  $1.25  $1.25  $1.50  $1.50  $1.50  $1.50  $1.50  $1.50  $1.50  $1.50  $1.50  $1.50
Popcorn           $0.25  $0.25  $0.25  $0.25  $0.30  $0.30  $0.60  $0.30  $0.30  $0.30  $0.30  $0.30  $0.30  $0.30  $0.30  $0.30  $0.30
Sunflower Seeds   $1.15  $1.15  $1.15  $1.15  $1.35  $1.35  $1.45  $1.45  $1.45  $1.45  $1.45  $1.45  $1.45  $1.45  $1.45  $1.45  $1.45
Salted-Smooth     $6.25  $7.50  $7.50  $7.50  $6.25  $6.25  $6.25  $6.25  $6.95  $6.25  $6.25  $6.25  $6.25  $6.25  $6.25  $6.25  $6.25
Salted-Chunky     $6.25  $7.50  $7.50  $7.50  $6.25  $6.25  $6.25  $6.25  $6.95  $6.25  $6.25  $6.25  $6.25  $6.25  $6.25  $6.25  $6.25
Nosalt-Smooth     $6.25  $7.50  $7.50  $7.50  $6.25  $6.25  $6.25  $6.25  $6.95  $6.25  $6.25  $6.25  $6.25  $6.25  $6.25  $6.25  $6.25
Nosalt-Chunky     $6.25  $7.50  $7.50  $7.50  $6.25  $6.25  $6.25  $6.25  $6.95  $6.25  $6.25  $6.25  $6.25  $6.25  $6.25  $6.25  $6.25



Reply to: