J
Jon Anderson
Hi,
Im having problems getting some Perl code to work (im a programming
newbie). The aim of the code is to read certain text from a file,
print it to screen then allow the user to make a choice based on that
output. Well, that's the aim so far anyway (im about half way through
the code). Ive been stuck on this however. Im getting the error "Use
of uninitialized value in print at... line 26, <STDIN> line 1."
Ive spent most of the day trying to work out what's wrong, but have
come up empty handed. The code is,
$lnum = 0;
open (INPUT, "file.txt") or die "Error, can't find file\n";
@raw_data=<INPUT>;
close(INPUT);
#Print to screen the pricelist in the text file
foreach $price (@raw_data)
{
@pricelist = split (/:/, $price);
print "$lnum" . " " . "$pricelist[1]\n" if @pricelist != 1;
$lnum ++;
}
print "\n";
$prices2 = @raw_data;
@prices1to5 = split (/:/, $prices2);
print "Please enter the item to be purchased..\n\n";
chomp($selectitem = <STDIN>);
if($selectitem == 1)
{
print "\n";
print "You selected item 1, which sells for\n";
print $prices1to5[1];
$item = 1;
}
elsif($selectitem == 2)
{
print "\n";
print "You selected item 2, which sells for\n";
print $prices1to5[2];
$item = 2;
}
elsif($selectitem == 3)
{
print "\n";
print "You selected item 3, which sells for\n";
print $prices1to5[3];
$item = 3;
}
elsif($selectitem == 4)
{
print "\n";
print "You selected item 4, which sells for\n";
print $prices1to5[4];
$item = 4;
}
elsif($selectitem == 5)
{
print "\n";
print "You selected item 5, which sells for\n";
print $prices1to5[5];
$item = 5;
}
else
{
print "\n";
print "Invalid item selection! No such item\n";
}
---
The code at line 26 is
18 $prices2 = @raw_data;
19 @prices1to5 = split (/:/, $prices2);
20 print "Please enter the item to be purchased..\n\n";
21 chomp($selectitem = <STDIN>);
22 if($selectitem == 1)
23 {
24 print "\n";
25 print "You selected item 1, which sells for\n";
26 print $prices1to5[1];
Im having problems getting some Perl code to work (im a programming
newbie). The aim of the code is to read certain text from a file,
print it to screen then allow the user to make a choice based on that
output. Well, that's the aim so far anyway (im about half way through
the code). Ive been stuck on this however. Im getting the error "Use
of uninitialized value in print at... line 26, <STDIN> line 1."
Ive spent most of the day trying to work out what's wrong, but have
come up empty handed. The code is,
$lnum = 0;
open (INPUT, "file.txt") or die "Error, can't find file\n";
@raw_data=<INPUT>;
close(INPUT);
#Print to screen the pricelist in the text file
foreach $price (@raw_data)
{
@pricelist = split (/:/, $price);
print "$lnum" . " " . "$pricelist[1]\n" if @pricelist != 1;
$lnum ++;
}
print "\n";
$prices2 = @raw_data;
@prices1to5 = split (/:/, $prices2);
print "Please enter the item to be purchased..\n\n";
chomp($selectitem = <STDIN>);
if($selectitem == 1)
{
print "\n";
print "You selected item 1, which sells for\n";
print $prices1to5[1];
$item = 1;
}
elsif($selectitem == 2)
{
print "\n";
print "You selected item 2, which sells for\n";
print $prices1to5[2];
$item = 2;
}
elsif($selectitem == 3)
{
print "\n";
print "You selected item 3, which sells for\n";
print $prices1to5[3];
$item = 3;
}
elsif($selectitem == 4)
{
print "\n";
print "You selected item 4, which sells for\n";
print $prices1to5[4];
$item = 4;
}
elsif($selectitem == 5)
{
print "\n";
print "You selected item 5, which sells for\n";
print $prices1to5[5];
$item = 5;
}
else
{
print "\n";
print "Invalid item selection! No such item\n";
}
---
The code at line 26 is
18 $prices2 = @raw_data;
19 @prices1to5 = split (/:/, $prices2);
20 print "Please enter the item to be purchased..\n\n";
21 chomp($selectitem = <STDIN>);
22 if($selectitem == 1)
23 {
24 print "\n";
25 print "You selected item 1, which sells for\n";
26 print $prices1to5[1];