http://www.perlmonks.org?node_id=344893

qball83 has asked for the wisdom of the Perl Monks concerning the following question:

I'm writing a flat-file publications databse CGI and I'm recieving an compilation error on the lines indicated below, any ideas??
Any help would be more than welcome.

if ($#results >= 0) { //line 135 foreach $record (@results) { chomp($record); ($author,$year,$title,$journal,$conf_name,$city,$volume,$issue,$pages, +$institution,$date,$type,$publisher,$edition,$translator,$scale,$call +_num,$label,$abstract,$notes,$isbn,$url,$editor,$keywords) = split(/\ +|/,$record); if ($title ne ""){ print "Title: $title\n";} if ($author ne ""){ print "Author: $author\n";} if ($year ne ""){ print "Year: $year\n";} if ($journal ne ""){ print "Journal: $journal\n";} if ($conf_name ne ""){ print "Conference: $conf_name\n";} if ($city ne ""){ print "City: $city";} if ($volume ne ""){ print "Volume: $volume\n";} if ($issue ne ""){ print "Issue: $issue\n";} if ($pages ne ""){ print "Pages: $pages\n";} if ($institution ne ""){ print "Institution: $institution\n";} if ($date ne ""){ print "Date: $date\n";} if ($type ne ""){ print "Type: $type\n";} if ($publisher ne ""){ print "Publisher: $publisher\n";} if ($edition ne ""){ print "Edition: $edition\n";} if ($translator ne ""){ print "Translator: $translator\n";} if ($scale ne ""){ print "Scale: $scale\n";} if ($call_num ne ""){ print "Call Number: $call_num\n";} if ($label ne ""){ print "Label: $label\n";} if ($abstract ne ""){ print "Abstract: $abstract\n";} if ($notes ne ""){ print "Notes: $notes\n";} if ($isbn ne ""){ print "ISBN/ISSN: $isbn\n";} if ($url ne ""){ print "URL: $url\n";} if ($editor ne ""){ print "Editor: $editor\n";} } //line 187 }
The errors are as follows:

syntax error at line 135 near ") {"
syntax error at line 187 near "}"

lines are marked in the scrip selection. Thanks!

Replies are listed 'Best First'.
Re: Execution/Compilation Error
by tedrek (Pilgrim) on Apr 13, 2004 at 23:53 UTC
    My guess is you forgot a semi-colon on line 134.
Re: Execution/Compilation Error
by Zaxo (Archbishop) on Apr 14, 2004 at 00:27 UTC

    I agree with tedrek++. The code you show looks fine for syntax.

    A few suggestions, however. Your line 135, the first shown, is true unless @results is empty. The usual way to write that is

    if (@results) { #... }
    That works because @results is in scalar context there, giving the number of elements. This decision is not strictly necessary, since the for loop will iterate zero times for an empty @results.

    The evident duplication of code in your long run of decisions suggests condensing that section into a loop. A hash for the data with the printed terms "Title".. as keys seems inviting, but suffers from not having a canonical order. Let's just impose that order by defining an array of those terms,

    my @columns = qw/ Title Author Year Journal # ... /; for my $record (@results) { my $index = 0; for (split /\|/, $record) { printf "%s: %s$/", $columns[$index], $_ if $_; $index++; } }
    That condensation separates data from logic, making the whole easier to maintain. It's not the last word, but a step in the right direction.

    Have you looked at any of the CSV modules?

    After Compline,
    Zaxo