Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IF Statement Syntax Error

Status
Not open for further replies.

mark1110

Programmer
Apr 20, 2005
85
US
Hi Again,

I have a print statement:

unless (print OVERRELATEFIL (
trim($overrelatefil{tin}),
trim($overrelatefil{parent_tin}),
trim($overrelatefil{paid_tin}),
trim($overrelatefil{sales_channel}),
trim($overrelatefil{eff_dt}),
trim($overrelatefil{term_dt}),
trim($overrelatefil{created_dt}),
trim($overrelatefil{created_userid}),
trim($overrelatefil{last_updated_dt}),
trim($overrelatefil{last_updated_userid}),
trim($overrelatefil{no_new_biz_eff_dt}),
trim($overrelatefil{rel_mbu_cd})
))

I want to add an if statement after the )) that will print a \n if the record is not the last record which I have a ||| in the $overrelatefil{tin} field. I am using this if statement:

if (OVERRELATEFIL ($overrelatefil{tin}) eq "|||") {
print "";
} else {
print "\n";
}

I am getting a syntax error with the if statement. I am looking at it and it looks okay. Can anyone see what I am doing wrong?

Thanks again

Mark
 
Code:
[kirsle@epsilon ~]$ perl -c
if (OVERRELATEFIL ($overrelatefil{tin}) eq "|||") {
    print "";
 } else {
    print "\n";
 }
__END__
- syntax OK

The syntax is ok.

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Why are you putting that print statement in an unless block?

OVERRELATEFIL is a filehandle and can't be used like that in an if statement.

Change it to:
if ( $overrelatefil{tin} eq "|||" ) {

Your trim sub is unnecessary and makes the script a little less efficient.

It would be better to strip the leading and trailing spaces as you assign the hash values.
 
I did that and I am still getting a syntax error. Here is the complete module, am I putting the if statement in the wrong place? This is an 15 year old piece of code I was asked to modify.



#
# nbr_FormatRelateRecord.pl
#
# nbr_FormatRelateRecord.pl 6.30.98 jhb

sub trim($)
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}


sub FormatOverRelateRecord
{

sub trim($);
local (%overrelatefil, $appLog) = @_;
my ($TRUE) = 1;
my ($FALSE) = 0;
my ($rc) = $FALSE;

local (%log) = ("logProcess", "",
"logType", "",
"logText", "",
"logFile", "");

$log{logFile} = $appLog;
$log{logProcess} = "nbr_FormatOverRelateRecord.pl";



my ($fileName) = $ENV{fullFileName};

unless(open (OVERRELATEFIL, ">>$fileName"))
{
&LogMsg ("E","Can\'t open file $fileName.");
return $FALSE;
}


unless (print OVERRELATEFIL (
trim($overrelatefil{tin}),
trim($overrelatefil{parent_tin}),
trim($overrelatefil{paid_tin}),
trim($overrelatefil{sales_channel}),
trim($overrelatefil{eff_dt}),
trim($overrelatefil{term_dt}),
trim($overrelatefil{created_dt}),
trim($overrelatefil{created_userid}),
trim($overrelatefil{last_updated_dt}),
trim($overrelatefil{last_updated_userid}),
trim($overrelatefil{no_new_biz_eff_dt}),
trim($overrelatefil{rel_mbu_cd})
))


if ($overrelatefil{tin} eq "|||"){
print "";
} else {
print "\n";
}


{
&LogMsg ("E","Can\'t write to file $fileName.");
return $FALSE;
}


unless (close(OVERRELATEFIL))
{
&LogMsg ("E","Can\'t close file $fileName.");
return $FALSE;
}

return $TRUE;

}

1;


The error message I am getting is:

syntax error at nbr_FormatOverRelateRecord.pl line 59, near ")

 
I'd need to see a sample of the input data and a better explanation of your goal.

But see if this change gives you what you want.

print OVERRELATEFIL (
trim($overrelatefil{tin}),
trim($overrelatefil{parent_tin}),
trim($overrelatefil{paid_tin}),
trim($overrelatefil{sales_channel}),
trim($overrelatefil{eff_dt}),
trim($overrelatefil{term_dt}),
trim($overrelatefil{created_dt}),
trim($overrelatefil{created_userid}),
trim($overrelatefil{last_updated_dt}),
trim($overrelatefil{last_updated_userid}),
trim($overrelatefil{no_new_biz_eff_dt}),
trim($overrelatefil{rel_mbu_cd})
);

print "\n" if $overrelatefil{tin} eq "|||";
 
What I am trying to do is to not have a CRLF for the last record I am printing out. I indicate what the last record is by |||. I am still getting a syntax error when I use either the if or just the print like you suggested.

If you can suggest a better way to do it I am open to suggestions.

Mark
 
There are a number of improvements that could and should be made, but first lets track down the syntax error.

I don't receive a syntax error with the change I suggested.

C:\test>perl -c mark1110.pl
mark1110.pl syntax OK


C:\test>type mark1110.pl
#
# nbr_FormatRelateRecord.pl
#
# nbr_FormatRelateRecord.pl 6.30.98 jhb

sub trim($)
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}


sub FormatOverRelateRecord
{

sub trim($);
local (%overrelatefil, $appLog) = @_;
my ($TRUE) = 1;
my ($FALSE) = 0;
my ($rc) = $FALSE;

local (%log) = ("logProcess", "",
"logType", "",
"logText", "",
"logFile", "");

$log{logFile} = $appLog;
$log{logProcess} = "nbr_FormatOverRelateRecord.pl";



my ($fileName) = $ENV{fullFileName};

unless(open (OVERRELATEFIL, ">>$fileName"))
{
&LogMsg ("E","Can\'t open file $fileName.");
return $FALSE;
}


print OVERRELATEFIL (
trim($overrelatefil{tin}),
trim($overrelatefil{parent_tin}),
trim($overrelatefil{paid_tin}),
trim($overrelatefil{sales_channel}),
trim($overrelatefil{eff_dt}),
trim($overrelatefil{term_dt}),
trim($overrelatefil{created_dt}),
trim($overrelatefil{created_userid}),
trim($overrelatefil{last_updated_dt}),
trim($overrelatefil{last_updated_userid}),
trim($overrelatefil{no_new_biz_eff_dt}),
trim($overrelatefil{rel_mbu_cd})
);

print "\n" if $overrelatefil{tin} eq "|||";


{
&LogMsg ("E","Can\'t write to file $fileName.");
return $FALSE;
}


unless (close(OVERRELATEFIL))
{
&LogMsg ("E","Can\'t close file $fileName.");
return $FALSE;
}

return $TRUE;

}

1;
 
Hi,

I found the syntax error, I had the if statement before:

{
&LogMsg ("E","Can\'t write to file $fileName.");
return $FALSE;
}

If I put it after it, I don't get the syntax error.

I am using this if statement:

if ($overrelatefil{tin} ne "|||") {
print "\n"
}

What is happening is that the print statement is printing to the screen instead of to the output file(a text file). Is there a way to put the if statement in the print OVERRELATEFIL () statement so the \n will print to the output file instead of the screen?

Thanks,

Mark
 
if ($overrelatefil{tin} ne "|||") {
print OVERRELATEFI "\n"
}
 
That did it! Thank you very much for all your help!!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top