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!

Syntax Error

Status
Not open for further replies.

mark1110

Programmer
Apr 20, 2005
85
US
Hi,

I am very very new to Perl. I inherited a program and I need to get rid of white spaces. I found a routine that will do it but when I try run it I get a syntax error. Here is the code:

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


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

sub FormatOverProdUseRecord
{

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

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

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



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

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


unless (printf OVERPRODUSEFIL ("%-11s" . # tin
"%-3s" . # location_cd
"%-5s" . # use_ind
"%-3s" . # use_st
"%-11s" . # eff_dt
"%-11s" . # end_dt
"%-11s" . # created_dt
"%-9s" . # created_userid
"%-11s" . # last_updated_dt
"%-8s\n" , # last_updated_userid
trim($overprodusefil{tin}),
trim($overprodusefil{location_cd}),
trim($overprodusefil{use_ind}),
trim($overprodusefil{use_st}),
trim($overprodusefil{eff_dt}),
trim($overprodusefil{end_dt}),
trim($overprodusefil{created_dt}),
trim($overprodusefil{created_userid}),
trim($overprodusefil{last_updated_dt}),
trim($overprodusefil{last_updated_userid})
))

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


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

return $TRUE;

}

;

All I did was to insert the trim function. Could someone please help me find the syntax error.

Thanks

Mark

 
The actual syntax error message would help. Do you have use warnings and/or use strict enabled?

I tried the trim() function in isolation and it seems to work fine.

Annihilannic.
 
There is no syntax error:

Code:
[kirsle@aerelon ~]$ perl -c
sub trim($)
{
    my $string = shift;
    $string =~ s/^\s+//;
    $string =~ s/\s+$//;
    return $string;
}

sub FormatOverProdUseRecord
{

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

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

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



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

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


  unless (printf OVERPRODUSEFIL ("%-11s"  .       # tin
           "%-3s"   .             # location_cd
           "%-5s"   .            # use_ind
           "%-3s"   .            # use_st
           "%-11s"  .            # eff_dt
           "%-11s"  .            # end_dt
           "%-11s"  .            # created_dt        
           "%-9s"   .            # created_userid
           "%-11s"  .            # last_updated_dt        
           "%-8s\n" ,                   # last_updated_userid
           trim($overprodusefil{tin}),
           trim($overprodusefil{location_cd}),
           trim($overprodusefil{use_ind}),
           trim($overprodusefil{use_st}),
           trim($overprodusefil{eff_dt}),
           trim($overprodusefil{end_dt}),
           trim($overprodusefil{created_dt}),
           trim($overprodusefil{created_userid}),
           trim($overprodusefil{last_updated_dt}),
           trim($overprodusefil{last_updated_userid})
           ))
  
  {
     &LogMsg ("E","Can\'t write to file $fileName.");
         return $FALSE;
  }


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

  return $TRUE;

}
__END__
- syntax OK

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Mark,

There's no syntax error in that code UNLESS you're running under strictures, which it's obvious that you're not using the strict pragma.

EVERY Perl script you write should include the strict and warnings pragmas i.e.,

use strict;
use warnings;

All non global vars that are declared with local should be declared with my instead to create lexical vars.

For example, this line:
local (%overprodusefil, $appLog) = @_;

should be changed to:
my ($appLog, %overprodusefil) = @_;

Note that I also swapped those vars. Let me know if you don't understand why.
 
I am sorry I should of posted the error message. Here is what I got:

Use of uninitialized value in string eq at nbr_Dump_OverProdUse.pl line 49

 
The script you posted was incomplete, so which line in that code is the line 49 that the warning is referring to?
 
Hi,

It turns out what I thought was an error message is just a warning message based on a null value being in a field. The problem I am having now is that the output sometimes works. Here is a sample of the output.

001529773S|01|ALL| | 01/01/2002|12/31/9999|01/11/2002|mu3789| 01/11/2002|mu3789
001529773S|01|RES| CA|01/01/2002|12/31/9999|01/11/2002|mu3789| 01/11/2002|mu3789
001562520S|01|ALL| | 11/01/1997|09/24/2002|09/15/2001|UBRPRC05|12/09/2002|mu8576
001562520S|02|ALL| | 09/25/2002|12/31/9999|09/25/2002|mu3324| 09/25/2002|mu3324
001562520S|02|PHYS|| 09/25/2002|12/31/9999|09/25/2002|mu3324| 09/25/2002|mu3324
001562520S|02|SPLY|| 09/25/2002|12/31/9999|09/25/2002|mu3324| 09/25/2002|mu3324

What I need is to be no spaces between the fields. It seems to work sometimes and not other times. Also, is there a way to check if the record is the last record to not use a carriage return '\n'.


Thanks,

Mark

 
is there a way to check if the record is the last record to not use a carriage return '\n'.

When I need something like this, I don't put the \n on *any* of the lines, I just put them all into an array... i.e.

Code:
# This:
my @output = ();
push (@output, "this is line 1");
push (@output, "this is line 2");
push (@output, "this is line 3");

# Instead of this:
my $output = '';
$output .= "This is line 1\n";
$output .= "This is line 2\n";
$output .= "This is line 3";

So instead of having \n's at the ends of your lines/records as you're collecting them, just put them into an array. In the end you join the array at "\n", which puts a \n between all lines (so, every line except for the very last one will end with a line break)

Code:
my $real_output = join ("\n", @output);

# this is how real_output looks now
print "[$real_output]";
__END__
[this is line 1
this is line 2
this is line 3]

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top