#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Generate sample data file for a given cobol copy book


    Hi ,
    I am beginner to Perl programming - I have just started with perl and have written a program which generates a sample input data file using a give cpp .. could you pl provide me suggestion to do it in better way ..

    My perl code :

    #!C:/Perl/bin/perl.exe -w

    # read contents of file
    use Path::Class;
    use autodie; # die if problem reading or writing a file

    sub trim($);

    # Perl trim function to remove whitespace from the start and end of the string
    sub trim($)
    {
    my $string = shift;
    $string =~ s/^\s+//;
    $string =~ s/\s+$//;
    return $string;
    }

    my $file = file('G:\perl\cobol.cpy');

    # read the content of the file
    my $content = $file ->slurp();

    # openr() returns an IO::File object to read from

    my $file_handle = $file->openr();

    # Read in line at a time
    $a = 0;
    while( my $line = $file_handle->getline() ) {
    #$line = substr($line,0,2);
    push(@coins,$line);
    $a += 1;

    }
    @mynumbers = (10,20,30,40,50);



    $l = 0;
    $l = length $coins[1];

    print trim($coins[1]);

    $l = length trim($coins[1]);
    $l -= 3;
    print "hello";
    print $l;


    $c = 1;
    open (MYFILE,'>>G:\perl\samp.txt');
    while ($a>0)
    {
    $b = 1;

    $l = 0;
    $l = length trim($coins[$c]);

    $l -= 3;
    print $l;
    $count = 0;
    $count = substr($coins[$c],$l+1,2);
    print $count;
    while ($b <= $count)
    {
    print MYFILE $c;
    $b += 1;
    }
    $a -= 1;
    $c += 1;
    }
    close(MYFILE);




    my cobol book:

    01 HOSPITAL.
    03 HOSPNAME PIC X(20).
    03 HOSP-ADDRESS PIC X(30).
    03 HOSP-PHONE PIC X(10).
    03 ADMIN PIC X(20).
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    835
    Rep Power
    496
    Hi,

    just a few comments.

    You should add the following pragmas:

    Perl Code:
    use strict;
    use warnings;


    and correct the warnings and errors that will be issued by the compiler (that will force you, among other things, to declare all your variables and to decide explicitly their scope).

    You should remove useless code (that will make your intent clearer), for example:

    Code:
    my $content = $file ->slurp();
    The $content variable is never used, so the code is useless.

    Similarly, the @mynumbers array is never used.

    In:
    Perl Code:
    $l = 0;
    $l = length $coins[1];
    print trim($coins[1]);
    $l = length trim($coins[1]);


    the two first lines are useless, since the value of $l is immediately overridden in the fourth line.

    Code:
    open (MYFILE,'>>G:\perl\samp.txt');
    This is an old and somewhat deprecated way to open a file. It is considered better practice to use this three-argument syntax:

    Perl Code:
    my $out_file = "output.txt";
    open(my $out_fh, '>>', $out_file) or die "could not open $out_file $! \n";


    (The "or die ..." part is not mandatory since you are using autodie, but, as a matter of personal taste, I would tend to prefer to control the error message that will be printed if the open instruction fails.)

    I am not sure what you really want to do with the $a, $b, $c and $l variables, but the way you use them looks quite clumsy and some of them are probably useless. Especially, $a and $c seem to be used only to iterate through your array, a foreach loop (rather than the while loop) would most probably be clearer and far more "perlish".

    There are probably many other things to say, but if you make the proposed changes and post your corrected code with the appropriate CODE tags in order to preserve the formatting (line indentation), it will be easier for us to read your code and understand what you are trying to do.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    835
    Rep Power
    496
    I forgot an important advice: use variable (and other identifier) names that give an idea of their content. For example, $array_size might be better than $a (but, as I said, you probably don't need this variable), $line_length better than $l, although it would be even better to find a name related to the content of the line.

IMN logo majestic logo threadwatch logo seochat tools logo