Reading value from File and using in another file

V

vedpsingh

I want to print something looking like this(binary value are in another
text file) :

rec_codein0 000000010000000000 0
rec_codein1 111101110000000000 0
.......
......
rec_codein15 111111110000000000 0
---------------------------------------------------------------------

I have written programmentioned below.
Value is being read in $_ but why can't I print in the desirable way
?
Please point out the mistake.

------------------------------------------------
use strict; # important pragma
use warnings; # another important pragma

open (MYFILERead, 'data.txt');
while (<MYFILERead>) {
chomp;
print "$_\n";
}

print "Enter value of instantiations ? "; # print out the question
chomp( my $j = <STDIN> );

for (my $i = 0; $i < $j; $i++) {

my $file = 'copypaste.txt';
open my $MYFILE, '>>', $file or die "Could not open '$file': $!\n";

print MYFILE "
rec_codein$i $_\n ";
}
close (MYFILE);
close (MYFILERead);
----------------------------------------------------------

File "data.txt" contains ------>

111110110000000000
111101110000000000
000000010000000000
000001100000000000
111110000000000000
000000010000000000
000010000000000000
000000100000000000
000000110000000000
111110000000000000
000000110000000000
000000000000000000
000001100000000000
111110100000000000
000000110000000000
111111110000000000

Thanks
 
P

Paul Lalli

Value is being read in $_ but why can't I print in the desirable way
?
Please point out the mistake.

------------------------------------------------
use strict; # important pragma
use warnings; # another important pragma

open (MYFILERead, 'data.txt');

I don't understand why you made the three standard mistakes in this
open when you did it correctly later on. Don't use global barewords,
use the three-argument form, and check for errors!
while (<MYFILERead>) {

Here, you read a line from the file into $_
chomp;
print "$_\n";
}

Here, at the end of each iteration, that value of $_ is thrown away.
You never saved it anywhere.
print "Enter value of instantiations ? "; # print out the question
chomp( my $j = <STDIN> );

for (my $i = 0; $i < $j; $i++) {

my $file = 'copypaste.txt';
open my $MYFILE, '>>', $file or die "Could not open '$file': $!\n";

Here, you're re-opening the file every time through the loop. Why?
Open it once.
print MYFILE "
rec_codein$i $_\n ";

Here, you're trying to print the value of $_. $_ does not contain
anything. It contained something only during each iteration of the
first while loop. After that while loop, that data was discarded.
}
close (MYFILE);
close (MYFILERead);

You need to re-order your programs into a logical sense.
Unfortunately, I have no idea what $i or $j have to do with the data
read from your data file, so I can't help you reorganize it.

Paul Lalli
 
V

vedpsingh

Hi,
Based on the suggestions, I made few changes.
First i thought to do the read and write in file independently.
Writting in file is not a problem.
But I am not able to do reading from a file . Please give your comment.
I am a beginer in perl.
Thanks

--------------------------------
use strict;
use warnings;
my $data_file = 'data.txt';
($data_file) = @ARGV;
open my $read_handle, '<', $data_file or die "Cannot open '$data_file':
";
{
print "@ARGV";
}
close ($read_handle);
----------------------------------

------Error message I am getting

C:\Perl\Myperl>perl read_NOT_DONE.pl
Use of uninitialized value in open at read_NOT_DONE.pl line 5.
Use of uninitialized value in concatenation (.) or string at
read_NOT_DONE.pl li
ne 5.
Cannot open '': at read_NOT_DONE.pl line 5.
 
D

David Squire

Hi,
Based on the suggestions, I made few changes.
First i thought to do the read and write in file independently.
Writting in file is not a problem.
But I am not able to do reading from a file . Please give your comment.
I am a beginer in perl.
Thanks

--------------------------------
use strict;
use warnings;
my $data_file = 'data.txt';
($data_file) = @ARGV;

Do you intend to clobber the value of $data_file here if no command line
argument was given? From the warnings and error messages below, it is
clear that $data_file is not initialized - because the first element of
@ARGV is undef. I'll bet you ran this with out a command line parameter.

It's not clear what you're trying to do, but it might be something like
this:

@ARGV and ($data_file) = @ARGV; # use first command line argument if
any are present.

(though this ignores any other command line arguments).

open my $read_handle, '<', $data_file or die "Cannot open '$data_file':
";
{
print "@ARGV";

What is this about? Why the enclosing braces? Why are you printing the
@ARGV array?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top