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
Solved

Print Problem

Posted on 2000-02-25
13
165 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Title # Comments Views Activity
perl script 4 111
perl script to check whether folder contains any files 5 92
instmodsh - listing perl modules 2 83
.properties file to call function/method 9 61
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 …
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…

840 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