5 Replies - 2887 Views - Last Post: 14 April 2009 - 04:31 PM

#1 Notorion   User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • Posts: 378
  • Joined: 17-February 09

While loop in Perl

Posted 13 April 2009 - 07:30 PM

Hi, essentially I'm getting this error when attempting to run my perl program, code is below


./cs216program4.pl
syntax error at ./cs216program4.pl line 63, near ")
{"
syntax error at ./cs216program4.pl line 68, near "}"
Execution of ./cs216program4.pl aborted due to compilation errors.


Also, if I'm doing something terribly wrong, let me know so I don't end up posting more questions back here, haha.
I'm used to c++, obviously different.


#!/usr/local/bin/perl
use strict;
#
# Author: Cameron Dixon
# Email: [email protected]
# Course: CS 216 Section 001
# Assignment: Program 4, perl section
#
#
# DESCRIPTION: This program is a recreation of a c++ program in perl, to get data from an input file
#		supplied by the specifications.  The files must be named "Book.data" and "Author.data"
#		the program is supposed to provide an interface to the user, to abstract specific
#		information about either the books or the authors with a search ID.  See post conditions
#		for more information about what specifically the program provides to the user.
#
# PRECONDITIONS: book.data and author.data must exist, and could hold good data, but doesn't have to
#		 The contents of the files also do not have to be in a pre sorted fashion.
#
# POSTCONDITIONS: The program will hold the data, in some form of a structure, either an array or hash.
#		The program will tell the user how many of each book and author the structure contains
#		The program also takes out the underscores in the titles of the books, for 
#		search and display.
#		The user will be presented a main menu, with 5 options from which they may select.
#
#		1 book data (by book code)
#
#		2 book data (by title)
#
#		3 author data (by author last name)
#
#		4 book data
#
#		5 end the program
#
#		and then the user may enter these options, and provide an ID for the search criteria
#		(except options 4 and 5)
#
# end of detailed explanation
#
##########################################################################################
#
# declare variables
my $word;  #key value for the hash table.
#
##########################################################################################
#
# open the file
# some code for this section was taken from example 1 of:
# http://www.cs.uky.edu/~paulp/CS216S09/CS216PerlExample.htm
# read in the book.data file, using a while loop
my $infile = "book.data";
if (!open(AINPUTF, "<$infile")) {
  print "error opening $infile\n";
  die;
}
my $ainput;
my $count=0;
my @splitBook;
my %store;
my $var
while(defined($ainput = <AINPUTF>))  
#this will return true if from keyboard or a file.
{  #error is here
  @splitBook = split(' ',$ainput);  #split each line by whitespace, and put them into an array, 
  $var = shift(@splitBook);  #  shift the first variable into a $var to use as a key for hash table
  $store{$var} = [$splitBook[0], $splitBook[1], $splitBook[2],$splitBook[3],$splitBook[4]];  #assign the rest of the data, into the key values of the hash table
  $count++;  #increment the count of the books, 
}  #and other error is this line
close(AINPUTF);

print "Total number of book records in file $infile is $count\n";  # show number of lines read in

# read in the author.data file, using a while loop
$infile = "author.data";
if (!open(BINPUTF, "<$infile")) {
  print "error opening $infile\n";
  die;
}
my $binput;
$count=0;
my @splitAuthor;
my %index;
$var
while (defined($binput = <BINPUTF>)) {  #this will return true if from keyboard or a file.
  @splitAuthor = split(' ',$binput);  #split each line by whitespace, and put them into an array, 
  $var = shift(@splitAuthor);  #  shift the first variable into a $var to use as a key for hash table
  $index{$var} = [$splitAuthor[0], $splitAuthor[1], $splitAuthor[2],$splitAuthor[3],$splitAuthor[4] ];  #assign the rest of the data, into the key values of the hash table
  $count++;  #increment the count of the authors, 
}

print "Total number of Authors in file $infile is $count\n";  # show number of lines read in

# use pattern matching here, for error checks
#   if doesn't meet requirements of program 4, die
#this is checking for bad file input on book.data
$infile = "book.data";
my $key;
#this loop is to check the book code for errors
$count = 0;
foreach $key (keys %store)
{
$count++;  #increment the count of which line in the hash it is on.
if(!$key =~ /[A-Z,\d]{4}/)  #any upper  case char or digit, for exactly 4 spaces
{
print "An error has been found on line $count in the file $infile\n";
print "The error is in the book code of $key\n";
print "A book code must contain, exactly 4 elements, uppercase letter and digits only\n";
}
}  #end for each loop

print "end for now";

#do error checking for author.data

# take the underscores out of the book titles, use pattern matching
#
#
# provide a looping main menu
#	book code search
#	book title search
#	author last name search
#	book data search (looping sorted books)
#	end the program option
#
# end of program.



Is This A Good Question/Topic? 0
  • +

Replies To: While loop in Perl

#2 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6659
  • View blog
  • Posts: 30,976
  • Joined: 10-May 07

Re: While loop in Perl

Posted 13 April 2009 - 07:36 PM

Did you try putting the bracket on the same line as your while statement? Looks like that's how all your if statements are, & those are working.

while(defined($ainput = <AINPUTF>))  {
#this will return true if from keyboard or a file.
  @splitBook = split(' ',$ainput);  #split each line by whitespace, and put them into an array,
  $var = shift(@splitBook);  #  shift the first variable into a $var to use as a key for hash table
  $store{$var} = [$splitBook[0], $splitBook[1], $splitBook[2],$splitBook[3],$splitBook[4]];  #assign the rest of the data, into the key values of the hash table
  $count++;  #increment the count of the books,
}  #and other error is this line




My guess would be that the comment between the while line & the bracket is causing the error.
Was This Post Helpful? 0
  • +
  • -

#3 KevinADC   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: While loop in Perl

Posted 13 April 2009 - 09:12 PM

find this line:

my $var

and this line

$var

and see if you can find the problem(s) with both of them

View Postno2pencil, on 13 Apr, 2009 - 06:36 PM, said:

My guess would be that the comment between the while line & the bracket is causing the error.


The error is staring you in the face:

my %store;
my $var
while(defined($ainput = <AINPUTF>))  
#this will return true if from keyboard or a file.
{  #error is here


This post has been edited by KevinADC: 13 April 2009 - 09:10 PM

Was This Post Helpful? 0
  • +
  • -

#4 dsherohman   User is offline

  • Perl Parson
  • member icon

Reputation: 227
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: While loop in Perl

Posted 14 April 2009 - 11:13 AM

View Postno2pencil, on 14 Apr, 2009 - 02:36 AM, said:

Did you try putting the bracket on the same line as your while statement? Looks like that's how all your if statements are, & those are working.

while(defined($ainput = <AINPUTF>))  {
#this will return true if from keyboard or a file.
  @splitBook = split(' ',$ainput);  #split each line by whitespace, and put them into an array,
  $var = shift(@splitBook);  #  shift the first variable into a $var to use as a key for hash table
  $store{$var} = [$splitBook[0], $splitBook[1], $splitBook[2],$splitBook[3],$splitBook[4]];  #assign the rest of the data, into the key values of the hash table
  $count++;  #increment the count of the books,
}  #and other error is this line




My guess would be that the comment between the while line & the bracket is causing the error.

Although I would recommend this change for stylistic reasons, the error has nothing to do with comment placement.

The error message is a bit misleading, though... The problem is actually on the line before the "while", not the lines containing ) or {.
Was This Post Helpful? 1
  • +
  • -

#5 Notorion   User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • Posts: 378
  • Joined: 17-February 09

Re: While loop in Perl

Posted 14 April 2009 - 01:15 PM

very misleading indeed.
my $var;  #added ';'


Was This Post Helpful? 0
  • +
  • -

#6 KevinADC   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: While loop in Perl

Posted 14 April 2009 - 04:31 PM

View PostNotorion, on 14 Apr, 2009 - 12:15 PM, said:

very misleading indeed.
my $var;  #added ';'



The error is only misleading if you don't understand the behavior of perl. You can write multi-line perl code so there is no requirement to have a semi-colon on the end of a line. Perl will continue to compile until it aboslutely can not continue to do so and will then report the place in the code where perl gave up trying.

You should also turn on the warnings pragma and for real beginners the diagnostics pragma is good too:

use warnings;
use diagnostics;

try this with and without diagnostics:

use strict;
use warnings;
use diagnostics;

my $var
while (my $line = <DATA>) {
   print $line;
}

__DATA__
Mary had
a little lamb


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1