Problem adding new line !

Problem adding new line !

am 03.04.2008 15:51:31 von WG

Hi,

I'm trying to add a new line to a file say "tmp_file1.txt" before
reading into that file. Here tmp_file1.txt is the destination file
into which I'm trying to copy.. Pasting code snippet. Is there
anything that I'm missing out here ..

sub create_tmp_file1()
{

# Pls note: the function recieve's the file handle of the
source file and passess on the file handle of the #destination file to
the sub routine that is called for inserting new line.. Attempt is to
give sufficient spacing #between reading from each source file..


my $inputFile = $_[0];
my $getLine;
open (TMP_FH, ">>tmp_file1.txt") or die "Unable to open file
tmp_file1 for writing ..\n";
print $inputFile."\n";

&addNewLine(\*TMP_FH);

while(<$inputFile>){

$getLine = $_;
print TMP_FH $getLine;
}

$getLine = "\x0C";
print TMP_FH $getLine;


# print TMP_FH "\x0c";

close (TMP_FH);

}

# Function that adds new line character..

sub addNewLine(){

my $fileName = $_[0];
my $count = 0;

# open TEMP_FH, ">>".$fileName or die "Failed opening $outputFile
file ...";

while($count le 30){
#print TEMP_FH "\n";
#print TEMP_FH " \n";
print $fileName " \n";
$count++;
}

# close(TEMP_FH);

}

Any pointer to this would be greatly appreciated..

-Wg

Re: Problem adding new line !

am 03.04.2008 16:22:19 von smallpond

On Apr 3, 9:51 am, Wg wrote:
> Hi,
>
> I'm trying to add a new line to a file say "tmp_file1.txt" before
> reading into that file. Here tmp_file1.txt is the destination file
> into which I'm trying to copy.. Pasting code snippet. Is there
> anything that I'm missing out here ..
>

You forgot to say "doesn't work" as the complete
explanation of what problem you might be having.

Re: Problem adding new line !

am 03.04.2008 16:36:04 von Ben Morrow

Quoth Wg :
>
> I'm trying to add a new line to a file say "tmp_file1.txt" before
> reading into that file. Here tmp_file1.txt is the destination file
> into which I'm trying to copy.. Pasting code snippet. Is there
> anything that I'm missing out here ..

What's going wrong? It's not clear exactly what you're trying to
achieve, and you haven't told us what the code below is doing wrong. It
appears to be appending the contents of $inputFile, followed by 31 lines
with two spaces, followed by a form-feed character (with no following
newline, so the file is no longer a text file) to tmp_file1.txt.

Some general comments on the code follow, but I doubt they'll solve your
problem.

> sub create_tmp_file1()

You don't want those parens: they don't do what you thing they do. Perl
isn't shell.

sub create_tmp_file1 {

> {
>
> # Pls note: the function recieve's the file handle of the
> source file and passess on the file handle of the #destination file to
> the sub routine that is called for inserting new line.. Attempt is to
> give sufficient spacing #between reading from each source file..
>
>
> my $inputFile = $_[0];
> my $getLine;

It's generally bad practice to declare variables earlier than you need
them.

> open (TMP_FH, ">>tmp_file1.txt") or die "Unable to open file
> tmp_file1 for writing ..\n";

You will find dealing with filehandles much easier if you use the
lexical filehandles introduced in perl 5.6. For a start, you won't need
to mess around with globrefs.

You should use three-arg open: while it doesn't matter in this case,
it's a good habit to get into.

open(my $TMP_FH, ">>", "tmp_file1.txt")
or die "...";

> print $inputFile."\n";
>
> &addNewLine(\*TMP_FH);

Don't call subs with & unless you know what it does. This may explain
why you got away with the () on your sub declarations...

With lexical filehandles (opened as above) you don't need to use globref
syntax. Just pass the filehandle as it is:

addNewLine($TMP_FH);

> while(<$inputFile>){
>
> $getLine = $_;

It's clearer to assign directly into the right variable. As a bonus, you
don't stomp on the global $_.

while (my $getLine = <$inputFile>) {

> print TMP_FH $getLine;
> }
>
> $getLine = "\x0C";
> print TMP_FH $getLine;

....of course, then $getLine is no longer in scope here. I would just go
back to printing it directly.

print $TMP_FH "\x0C";

> # print TMP_FH "\x0c";
>
> close (TMP_FH);

Another benefit of lexical filehandles is that they close for you when
the variable goes out of scope. When you're writing to a file, though,
you should check for errors on close, as any error while writing
(because of a full disk, for instance) will show up as an error on close.
(Of course, if you want to know exactly *what* failed to write, as
opposed to simply dieing with an error, you'll need to check the return
value of print as well.)

> }
>
> # Function that adds new line character..
>
> sub addNewLine(){
>
> my $fileName = $_[0];

This is not a file name, but a filehandle. I presume you know this, but
leaving bad variable names in your code is a sure way to confuse
yourself when you come back to it later (not to mention people on Usenet
trying to read your code).

> my $count = 0;
>
> # open TEMP_FH, ">>".$fileName or die "Failed opening $outputFile
> file ...";
>
> while($count le 30){

It's simpler to write this as a for loop (foreach if you prefer):

for my $count (0..30) {

Ben

Re: Problem adding new line !

am 03.04.2008 19:22:32 von WG

On Apr 3, 7:36=A0pm, Ben Morrow wrote:
> Quoth Wg :
>
>
>
> > I'm trying to add a new line to a file say "tmp_file1.txt" before
> > reading into that file. Here tmp_file1.txt is the destination file
> > into which I'm trying to copy.. Pasting code snippet. Is there
> > anything that I'm missing out here ..
>
> What's going wrong? It's not clear exactly what you're trying to
> achieve, and you haven't told us what the code below is doing wrong. It
> appears to be appending the contents of $inputFile, followed by 31 lines
> with two spaces, followed by a form-feed character (with no following
> newline, so the file is no longer a text file) to tmp_file1.txt.
>
> Some general comments on the code follow, but I doubt they'll solve your
> problem.
>
> > sub create_tmp_file1()
>
> You don't want those parens: they don't do what you thing they do. Perl
> isn't shell.
>
> =A0 =A0 sub create_tmp_file1 {
>
> > {
>
> > =A0 =A0 =A0 =A0# Pls note: the function recieve's the file handle of the=

> > source file and passess on the file handle of the #destination file to
> > the sub routine that is called for inserting new line.. Attempt is to
> > give sufficient spacing =A0 #between reading from each source file..
>
> > =A0 =A0my $inputFile =3D $_[0];
> > =A0 =A0my $getLine;
>
> It's generally bad practice to declare variables earlier than you need
> them.
>
> > =A0 =A0open (TMP_FH, ">>tmp_file1.txt") or die "Unable to open file
> > tmp_file1 for writing ..\n";
>
> You will find dealing with filehandles much easier if you use the
> lexical filehandles introduced in perl 5.6. For a start, you won't need
> to mess around with globrefs.
>
> You should use three-arg open: while it doesn't matter in this case,
> it's a good habit to get into.
>
> =A0 =A0 open(my $TMP_FH, ">>", "tmp_file1.txt")
> =A0 =A0 =A0 =A0 or die "...";
>
> > =A0 =A0print $inputFile."\n";
>
> > =A0 =A0&addNewLine(\*TMP_FH);
>
> Don't call subs with & unless you know what it does. This may explain
> why you got away with the () on your sub declarations...
>
> With lexical filehandles (opened as above) you don't need to use globref
> syntax. Just pass the filehandle as it is:
>
> =A0 =A0 addNewLine($TMP_FH);
>
> > =A0 =A0while(<$inputFile>){
>
> > =A0 =A0 =A0 =A0 =A0 =A0$getLine =3D $_;
>
> It's clearer to assign directly into the right variable. As a bonus, you
> don't stomp on the global $_.
>
> =A0 =A0 while (my $getLine =3D <$inputFile>) {
>
> > =A0 =A0 =A0 =A0 =A0 =A0print TMP_FH $getLine;
> > =A0 =A0}
>
> > =A0 =A0$getLine =3D "\x0C";
> > =A0 =A0print TMP_FH $getLine;
>
> ...of course, then $getLine is no longer in scope here. I would just go
> back to printing it directly.
>
> =A0 =A0 print $TMP_FH "\x0C";
>
> > # =A0print TMP_FH "\x0c";
>
> > =A0 =A0close (TMP_FH);
>
> Another benefit of lexical filehandles is that they close for you when
> the variable goes out of scope. When you're writing to a file, though,
> you should check for errors on close, as any error while writing
> (because of a full disk, for instance) will show up as an error on close.
> (Of course, if you want to know exactly *what* failed to write, as
> opposed to simply dieing with an error, you'll need to check the return
> value of print as well.)
>
> > }
>
> > # Function that adds new line character..
>
> > sub addNewLine(){
>
> > =A0 =A0my $fileName =3D $_[0];
>
> This is not a file name, but a filehandle. I presume you know this, but
> leaving bad variable names in your code is a sure way to confuse
> yourself when you come back to it later (not to mention people on Usenet
> trying to read your code).
>
> > =A0 =A0my $count =3D 0;
>
> > # =A0open TEMP_FH, ">>".$fileName or die "Failed opening $outputFile
> > file ...";
>
> > =A0 =A0while($count le 30){
>
> It's simpler to write this as a for loop (foreach if you prefer):
>
> =A0 =A0 for my $count (0..30) {
>
> Ben

Thanks Ben !

I was trying to insert new line to the destination file between
reading each source file. Follow'd Ben's guidance on this.. Pasting
below the code snippet that worked for me..

sub create_tmp_file1{

open (my $TMP_FH,">>","tmp_file1.txt") or die "Unable to open file
tmp_file1 for writing ..\n";

addNewLine($TMP_FH);

my $inputFile =3D $_[0];
my $getLine;

while(<$inputFile>){

$getLine =3D $_;
print $TMP_FH $getLine;
}

print $TMP_FH "\x0C";

close ($TMP_FH);

}

sub addNewLine{

my $fileHandle =3D $_[0];

# open ($fileHandle, ">>", "tmp_file1.txt") or die "Failed opening
file ...";

for my $count(0 .. 10){

print $fileHandle " \n";

}

# close($fileHandle);

}

Thanks Ben for your time and patience..

-Wg

Re: Problem adding new line !

am 03.04.2008 21:20:40 von someone

Wg wrote:
>
> I was trying to insert new line to the destination file between
> reading each source file. Follow'd Ben's guidance on this.. Pasting
> below the code snippet that worked for me..
>
> sub create_tmp_file1{
>
> open (my $TMP_FH,">>","tmp_file1.txt") or die "Unable to open file
> tmp_file1 for writing ..\n";
>
> addNewLine($TMP_FH);
>
> my $inputFile = $_[0];
> my $getLine;
>
> while(<$inputFile>){
>
> $getLine = $_;
> print $TMP_FH $getLine;
> }
>
> print $TMP_FH "\x0C";
>
> close ($TMP_FH);
>
> }
>
> sub addNewLine{
>
> my $fileHandle = $_[0];
>
> # open ($fileHandle, ">>", "tmp_file1.txt") or die "Failed opening
> file ...";

You had already opened the filehandle in create_tmp_file1() above so you
don't have to open it again.


> for my $count(0 .. 10){
>
> print $fileHandle " \n";
>
> }

Or more simply:

print $fileHandle " \n" x 11;

Or just replace the line:

addNewLine($TMP_FH);

in create_tmp_file1() above with the line:

print $TMP_FH " \n" x 11;


> # close($fileHandle);
>
> }


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall