Reading value from File and using in another file

Discussion in 'Perl Misc' started by vedpsingh@gmail.com, Aug 4, 2006.

  1. Guest

    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
     
    , Aug 4, 2006
    #1
    1. Advertising

  2. Paul Lalli Guest

    wrote:
    > 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
     
    Paul Lalli, Aug 4, 2006
    #2
    1. Advertising

  3. Guest

    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.
     
    , Aug 5, 2006
    #3
  4. David Squire Guest

    wrote:
    > 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?

    > }
    > 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.
    >
     
    David Squire, Aug 5, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. leon
    Replies:
    0
    Views:
    444
  2. Pierre McCann

    comparing pointed-at value with another value

    Pierre McCann, Oct 10, 2003, in forum: C Programming
    Replies:
    6
    Views:
    359
    Mark McIntyre
    Oct 10, 2003
  3. Karim Ali

    Reading a file and resuming reading.

    Karim Ali, May 25, 2007, in forum: Python
    Replies:
    2
    Views:
    388
    Hrvoje Niksic
    May 25, 2007
  4. Robert Cohen
    Replies:
    3
    Views:
    295
    Andrew Durstewitz
    Jul 15, 2003
  5. Replies:
    4
    Views:
    554
    cwdjrxyz
    Jan 17, 2006
Loading...

Share This Page