amitsh has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I need to update a file1 for some data, wherein the data contains in the file2. e.g File1 contains info for the Design name and area for that design which is sequentially given as Design area= ; Design area= ; . . . Design area=; and so on... File 2 contains 12 14 56 .... so on. so how to fix the values of file2 in the file1 for the area info. can anybody please help me with this?
  • Comment on Updating the files with data from another file

Replies are listed 'Best First'.
Re: Updating the files with data from another file
by cdarke (Prior) on Nov 26, 2010 at 09:59 UTC
    This seems like a simple merge programming exercise, and I don't see anything that is Perl specific in the question.

    Open both files for read
    Open a third file as the output file - this will be the new version of file1
    Within a loop
    Read a record from File1
    Read a record from File2
    Construct a new record from these two values
    Write out the new record to the output file
    Close the files.
Re: Updating the files with data from another file
by mjscott2702 (Pilgrim) on Nov 26, 2010 at 09:06 UTC
    Some sample (dummy) data and expected output would be useful, difficult to tell what exactly you are asking for - also, you will get better responses by posting any code you have tried so far.
Re: Updating the files with data from another file
by tospo (Hermit) on Nov 26, 2010 at 09:55 UTC
    Yes, it's really difficult to tell what exactly you want to achieve but you should explore tasks such as reading a file into a hash or array. If you just have tags in one file, one per line, and the associated values are in the second file, also one per line, then you could simply read one line from file 1 to get the tag, then read one line from file 2 and print out the tag=value pair. In that case, you don't even need to store anything in a data structure.
    Of course it should also be mentioned that you should probably look into the program that generated such data in the first place because the potential for associating the wrong things is huge with this setup.
Re: Updating the files with data from another file
by Marshall (Canon) on Nov 26, 2010 at 22:43 UTC
    how to fix the values of file2 in the file1?

    There may be some misunderstanding... You cannot "fix file1". You must make a new file which is the "fixed" version of file1. Then you can adjust the file names such that this new file replaces the existing file1.

    Stuff like say some area number cannot be directly added into file1(i.e., area=1234). The file on disk is literally a very long string of bits. You can't add more bits between say bit #38 and bit #39 without overwriting what was in bits #39,40,...etc. Think of an old cassette tape with 5 songs. You can't add a new song #6 between song 2 and 3 without rewriting songs 3,4,5.

    cdarke's post is the correct method for this procedure.

    So, you have the general idea of what needs to be done. Write some code and report back with it and specific questions.