Solved

Print issue and Error

Posted on 2000-02-24
6
153 Views
Last Modified: 2012-05-04
Following is how the code stands:

I am receiving an error

Value of <HANDLES> construct can be "0"; test with defined () at line 65535

and have no idea what the error is referrring to.


Also the output puts a | after the last field which distrurbs bcp in sybase.
How would you change the print statement to not put a | only after the last Part.
Otherwise a | should go after a part.

#! /usr/local/bin/perl


open(RAW, "< /data/oag/jan/oag.0199.dat");
unlink "left_over.txt";
open(LEFT, "> /data/oag/jan/left_over.txt");
open(OUT, "> /data/oag/jan/oag.0199.dbload");

while ($Line = <RAW>)
{
                     
      # Add a leading character so that awk's 1-based
      # second substr parameter will work
      $Line =  " " . $Line;
      $SendLineToLeftover = 0;   #FALSE
      chomp $Line;
      $i = 1;
                       
        $Part[$i] = substr($Line,1,4);  $i++;
        $Part[$i] = substr($Line,5,4);  $i++;
        $Part[$i] = substr($Line,10,4); $i++;
        $Part[$i] = substr($Line,14,4); $i++;
        $Part[$i] = substr($Line,18,4); $i++;
        $Part[$i] = substr($Line,22,4); $i++;
        $Part[$i] = substr($Line,27,4); $i++;
        $Part[$i] = substr($Line,31,1); $i++;
        $Part[$i] = substr($Line,32,4); $i++;
        $Part[$i] = substr($Line,36,4); $i++;
        $Part[$i] = substr($Line,40,1); $i++;
        $Part[$i] = substr($Line,41,3); $i++;
        $Part[$i] = substr($Line,44,5); $i++;
        $Part[$i] = substr($Line,54,1); $i++;
        $Part[$i] = substr($Line,55,1); $i++;
        $Part[$i] = substr($Line,56,1); $i++;
        $Part[$i] = substr($Line,57,1); $i++;
        $Part[$i] = substr($Line,58,1); $i++;
        $Part[$i] = substr($Line,59,1); $i++;
        $Part[$i] = substr($Line,60,1); $i++;
        $Part[$i] = substr($Line,61,1); $i++;
        $Part[$i] = substr($Line,62,1); $i++;
        $Part[$i] = substr($Line,63,4); $i++;
        $Part[$i] = substr($Line,67,4); $i++;
        $Part[$i] = substr($Line,71,4); $i++;
        $Part[$i] = substr($Line,75,3); $i++;
        $Part[$i] = substr($Line,78,3); $i++;
        $Part[$i] = substr($Line,81,6); $i++;
        $Part[$i] = substr($Line,87,6); $i++;
        $Part[$i] = substr($Line,93,6); $i++;
        $Part[$i] = substr($Line,99,6); $i++;
        $Part[$i] = substr($Line,105,3); $i++;
        $Part[$i] = substr($Line,109,3); $i++;
        $Part[$i] = substr($Line,113,2); $i++;
        $Part[$i] = substr($Line,115,1); $i++;
        $Part[$i] = substr($Line,116,3); $i++;
        $Part[$i] = substr($Line,119,1); $i++;
        $Part[$i] = " ";               $i++;
        $Part[$i] = substr($Line,124,27); $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
      $Part[$i] = " ";               $i++;
        $LastPartPlus1 = $i;

                 if ($Part[29] =~ /[0-9]{5}[KROM\{]/) {
                         $Part[29] = "+" . substr($Part[29],0,5);
                }
                elsif ($Part[29] =~ /[0-9]{5}[ABCDEFGHIJLNPQ\}]/) {
                         $Part[29] = "-" . substr($Part[29],0,5);
                }

                else {
                         $SendLineToLeftover = 1;   #TRUE
                }

                if ($Part[31] =~ /[0-9]{5}[KROM\{]/) {
                            $Part[31] = "+" . substr($Part[31],0,5);
                }
                elsif ($Part[31] =~ /[0-9]{5}[ABCDEFGHIJLNPQ\}]/) {
                            $Part[31] = "-" . substr($Part[31],0,5);
                }

                else {
                            $SendLineToLeftover = 1;   #TRUE
                }
                if ($SendLineToLeftover) {
                         #
                         # Send the left overs to a file.
                         #
                         print LEFT substr($Line, 1), "\n";   # Remove the space added earlier
                }
                 else {
                         #
                         # Print the other lines (modified) to standard out.
                         #
                         # Create the new line from its parts
                         $NewLine = "";
                         for ($i = 1; $i < $LastPartPlus1; $i++) {
                            $NewLine = $NewLine . $Part[$i] . "|";
                         }
                         print OUT $NewLine." \n";
                      }
                }

close LEFT;
close RAW;
close OUT;



#! /usr/local/bin/perl -w


open(RAW, "< /data/oag/feb/oag.299.dat");
unlink "left_over.txt";
open(LEFT, "> /data/oag/feb/left_over.txt");
open(OUT, "> /data/oag/feb/oag.299.dbload");

while ($Line = <RAW>)
{
                     
      # Add a leading character so that awk's 1-based
      # second substr parameter will work
      $Line =  " " . $Line;
      $SendLineToLeftover = 0;   #FALSE
      chomp $Line;
      $i = 1;
                       
        $Part[$i] = substr($Line,1,4);  $i++;
        $Part[$i] = substr($Line,5,4);  $i++;
        $Part[$i] = substr($Line,10,4); $i++;
        $Part[$i] = substr($Line,14,4); $i++;
        $Part[$i] = substr($Line,18,4); $i++;
        $Part[$i] = substr($Line,22,4); $i++;
        $Part[$i] = substr($Line,27,4); $i++;
        $Part[$i] = substr($Line,31,1); $i++;
        $Part[$i] = substr($Line,32,4); $i++;
        $Part[$i] = substr($Line,36,4); $i++;
        $Part[$i] = substr($Line,40,1); $i++;
        $Part[$i] = substr($Line,41,3); $i++;
        $Part[$i] = substr($Line,44,5); $i++;
        $Part[$i] = substr($Line,54,1); $i++;
        $Part[$i] = substr($Line,55,1); $i++;
        $Part[$i] = substr($Line,56,1); $i++;
        $Part[$i] = substr($Line,57,1); $i++;
        $Part[$i] = substr($Line,58,1); $i++;
        $Part[$i] = substr($Line,59,1); $i++;
        $Part[$i] = substr($Line,60,1); $i++;
        $Part[$i] = substr($Line,61,1); $i++;
        $Part[$i] = substr($Line,62,1); $i++;
        $Part[$i] = substr($Line,63,4); $i++;
        $Part[$i] = substr($Line,67,4); $i++;
        $Part[$i] = substr($Line,71,4); $i++;
        $Part[$i] = substr($Line,75,3); $i++;
        $Part[$i] = substr($Line,78,3); $i++;
        $Part[$i] = substr($Line,81,6); $i++;
        $Part[$i] = substr($Line,87,6); $i++;
        $Part[$i] = substr($Line,93,6); $i++;
        $Part[$i] = substr($Line,99,6); $i++;
        $Part[$i] = substr($Line,105,3); $i++;
        $Part[$i] = substr($Line,109,3); $i++;
        $Part[$i] = substr($Line,113,2); $i++;
        $Part[$i] = substr($Line,115,1); $i++;
        $Part[$i] = substr($Line,116,3); $i++;
        $Part[$i] = substr($Line,119,1); $i++;
        $Part[$i] = substr($Line,120,27); $i++;
        $Part[$i] = substr($Line,147,27); $i++;
      $Part[$i] = substr($Line,174,3); $i++;
      $Part[$i] = substr($Line,177,5); $i++;
      $Part[$i] = substr($Line,182,27); $i++;
      $Part[$i] = substr($Line,209,27); $i++;
      $Part[$i] = substr($Line,236,27); $i++;
      $Part[$i] = substr($Line,263,2); $i++;
      $Part[$i] = substr($Line,265,2); $i++;
      $Part[$i] = substr($Line,267,1); $i++;
      $Part[$i] = substr($Line,268,1); $i++;
      $Part[$i] = substr($Line,269,1); $i++;
      $Part[$i] = substr($Line,270,1); $i++;
      $Part[$i] = substr($Line,271,1); $i++;
      $Part[$i] = substr($Line,272,1); $i++;
      $Part[$i] = substr($Line,273,1); $i++;
      $Part[$i] = substr($Line,274,1); $i++;
      $Part[$i] = substr($Line,275,1); $i++;
      $Part[$i] = substr($Line,276,1); $i++;
      $Part[$i] = substr($Line,277,1); $i++;
      $Part[$i] = substr($Line,278,1); $i++;
      $Part[$i] = substr($Line,279,1); $i++;
      $Part[$i] = substr($Line,280,1); $i++;
      $Part[$i] = substr($Line,281,1); $i++;
      $Part[$i] = substr($Line,282,1); $i++;
      $Part[$i] = substr($Line,283,1); $i++;
      $Part[$i] = substr($Line,284,1); $i++;
      $Part[$i] = substr($Line,285,1); $i++;
      $Part[$i] = substr($Line,286,1); $i++;
      $Part[$i] = substr($Line,287,1); $i++;
      $Part[$i] = substr($Line,288,1); $i++;
      $Part[$i] = substr($Line,289,1); $i++;
      $Part[$i] = substr($Line,290,1); $i++;
      $Part[$i] = substr($Line,291,1); $i++;
      $Part[$i] = substr($Line,292,1); $i++;
      $Part[$i] = substr($Line,293,1); $i++;
      $Part[$i] = substr($Line,294,1); $i++;
      $Part[$i] = substr($Line,295,1); $i++;
      $Part[$i] = substr($Line,296,1); $i++;
      $Part[$i] = substr($Line,297,1); $i++;
      $Part[$i] = substr($Line,298,3); $i++;
        $LastPartPlus1 = $i;

                 if ($Part[29] =~ /[0-9]{5}[KROM\{]/) {
                         $Part[29] = "+" . substr($Part[29],0,5);
                }
                elsif ($Part[29] =~ /[0-9]{5}[ABCDEFGHIJLNPQ\}]/) {
                         $Part[29] = "-" . substr($Part[29],0,5);
                }

                else {
                         $SendLineToLeftover = 1;   #TRUE
                }

                if ($Part[31] =~ /[0-9]{5}[KROM\{]/) {
                            $Part[31] = "+" . substr($Part[31],0,5);
                }
                elsif ($Part[31] =~ /[0-9]{5}[ABCDEFGHIJLNPQ\}]/) {
                            $Part[31] = "-" . substr($Part[31],0,5);
                }

                else {
                            $SendLineToLeftover = 1;   #TRUE
                }
                if ($SendLineToLeftover) {
                         #
                         # Send the left overs to a file.
                         #
                         print LEFT substr($Line, 1), "\n";   # Remove the space added earlier
                }
                 else {
                         #
                         # Print the other lines (modified) to standard out.
                         #
                         # Create the new line from its parts
                         $NewLine = "";
                         for ($i = 1; $i < $LastPartPlus1; $i++) {
                            $NewLine = $NewLine . $Part[$i] . "|";
                         }
                         print OUT $NewLine." \n";
                      }
                }

close LEFT;
close RAW;
close OUT;
0
Comment
Question by:tomatocans
6 Comments
 

Author Comment

by:tomatocans
ID: 2556059
Sorry Code listed 2x
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2556097
i think the problem is with the raw data file. Check line 65535 of /data/oag/jan/oag.0199.dat. you will find some special character that PERL is not treating as raw data, but trying to interpret as a PERL directive.

the solution would be to escape that character to PERL treats it as raw data.
0
 
LVL 84

Expert Comment

by:ozo
ID: 2556190
/usr/local/bin/perl -Mdiagnostics -wc
while ($Line = <RAW>){}
Value of <HANDLE> construct can be "0"; test with defined() at - line 1 (#1)
   
    (W) In a conditional expression, you used <HANDLE>, <*> (glob), each(),
    or readdir() as a boolean value.  Each of these constructs can return a
    value of "0"; that would make the conditional expression false, which is
    probably not what you intended.  When using these constructs in conditional
    expressions, test their values with the defined operator.
   
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 2

Accepted Solution

by:
mattrope earned 10 total points
ID: 2556203
Value of <HANDLES> construct can be "0"; test with defined () at line 65535

refers to this line:

  while ($Line = <RAW>)

It means that it is possible that you would receive the value "0" from the filehandle under some circumstances.  This would be treated as a FALSE value, so the loop would stop, even if there was more stuff in the file.  This generally won't happen unless you're changing the line termination character...

Anyway, it's just a warning, not an error that has to be fixed.  To fix it, change the line to:

  while (defined($Line = <RAW>))

Because 0 or " " or whatever will always be a defined value, even if it is a false value.

As for not displaying the "|" on the last line, just loop until ($LastPartPlus1 - 1) and then display the last line outside the loop, without the |.

Hope this clears up your confusion.
0
 

Author Comment

by:tomatocans
ID: 2558835
how would u change the code to make the loop work for displaying purposes
0
 

Author Comment

by:tomatocans
ID: 2558909
Thanks. Right on the money with the define correction.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question