?
Solved

Print Problem

Posted on 2000-02-25
13
Medium Priority
?
169 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 20 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
 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Six Sigma Control Plans

764 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