Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 21st, 2012, 01:20 AM
poornimaj85 poornimaj85 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 1 poornimaj85 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 10 sec
Reputation 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).

Reply With Quote
  #2  
Old October 21st, 2012, 04:39 AM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 513 Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 4 Days 20 h 27 m 59 sec
Reputation Power: 405
Hi,

just a few comments.

You should add the following pragmas:

Perl Code:
Original - Perl Code
  1. use strict;
  2. 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:
Original - Perl Code
  1. $l = 0;
  2. $l = length $coins[1];
  3. print trim($coins[1]);
  4. $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:
Original - Perl Code
  1. my $out_file = "output.txt";
  2. 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.

Reply With Quote
  #3  
Old October 21st, 2012, 05:42 AM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 513 Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 4 Days 20 h 27 m 59 sec
Reputation Power: 405
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Generate sample data file for a given cobol copy book

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap