Solved

Print Problem

Posted on 2000-02-25
13
161 Views
Last Modified: 2010-03-05
Following is the code for the print statement.

I am trying to get it to print a | after every Part except the last one.



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-1; $i++) {
                            $NewLine = $NewLine . $Part[$i] . "|";
                         }
                         print OUT $NewLine." \n";
                      }
                }
0
Comment
Question by:tomatocans
13 Comments
 
LVL 3

Expert Comment

by:RobWMartin
ID: 2558953
Instead of the for loop, you could just use join:

$NewLine=join("|",@Part);

or maybe I don't understand the intent of the script.

0
 
LVL 3

Expert Comment

by:RobWMartin
ID: 2558964
Actually, it looks like you are only interested in @Part from 1 to $LastPartPlus1-2, so

$NewLine=join("|",@Part[1..$LastPartPlus1-2]);

0
 

Author Comment

by:tomatocans
ID: 2559158
Rob,

Following is the whole script:


#! /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 (defined($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;



0
 
LVL 84

Expert Comment

by:ozo
ID: 2559181
You could replace a lot of your $Part[$i] = substr statements with an unpack function
0
 

Author Comment

by:tomatocans
ID: 2559221
what's an unpack function
0
 
LVL 3

Accepted Solution

by:
RobWMartin earned 5 total points
ID: 2559376
Good idea, ozo.

Tom,  unpack allows you to break up a string that has structure by specifying the structure in a template. For example,

$str="ThisThatandtheOther";
@p=unpack("A4A4A3A3A5",$str);
print join(" ",@p);

This will split the words into an array and then print the sentence with proper spacing.  The point is that you specify the size of the type of field with the letter (A means ascii, in this case) and the size of the field follows.  So, A4 means take the next 4 ascii characters.

Regarding the question, I think the join will work with one mod:

$NewLine=join("|",@Part[1..#@Part]);

Rob



0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:tomatocans
ID: 2559549
replaced

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";
                                         }
                                   }

with


else {
                                            #
                                            # Print the other lines (modified) to standard out.
                                            #
                                            # Create the new line from its parts
                                            $NewLine = "";
                                            $NewLine=join("|",@Part[1..#@Part]);
                                            print OUT $NewLine." \n";
                                         }
                                   }

keep getting missing right brack error
0
 
LVL 3

Expert Comment

by:RobWMartin
ID: 2559588
Best I can tell, you need to get rid of the second to last bracket
0
 

Author Comment

by:tomatocans
ID: 2559631
Getting more syntax errors:

#! /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 (defined($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;


can u say exactly how u would chnage this code to eliminate the last |

thanks
0
 
LVL 3

Expert Comment

by:RobWMartin
ID: 2559710
The last comment is the original script?  Has it ever worked without syntax errors?  Need to see the actual error statement(s) generated by perl.  I don't have time, right now, to rewrite the script for you.  As far as I can tell, the second to last bracket, right after the print OUT $NewLine."\n";, needs to be deleted.
0
 

Author Comment

by:tomatocans
ID: 2559905
The only problem with the last sript is that a | is placed after the last field of output
0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 2561562
chop $NewLine;
print OUT $NewLine." \n";
0
 

Author Comment

by:tomatocans
ID: 2562489
Thanks
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now