in reply to Re^6: write to Disk instead of RAM without using modules
in thread write to Disk instead of RAM without using modules

The following code reads a file line by line:

my $filename = 'some/filename.txt'; open my $fh, '<', $filename or die "Couldn't read '$filename': $!"; while (<$fh>) { ... }

If you want to store information about a file, do so while reading it line by line.

Replies are listed 'Best First'.
Re^8: write to Disk instead of RAM without using modules
by Anonymous Monk on Oct 25, 2016 at 08:36 UTC
    I need to open and compare multiple files and not open just one file. So I have to open files sequentially and compare. I am unable to get that through.

      Maybe you can repeat the code to open one time a second time and give it a different filename this time?

      Maybe now is a good time to revisit some introductory material on Perl. Maybe Modern Perl helps you. It is even available at no cost as a PDF download.

      I am sorry for posting again and again but as suggested by Laurent_R, load one file in memory and read all other files sequentially. I am unable to get through that as how to load one file in memory and open rest of the file sequentially for comparison.

        My code is amenable for both, storing the data of a file in memory and working sequentially through a file.

        Note that "storing a file in memory" is easily achieved using an array as storage and reading each line from a file sequentially and storing it in the array.

      can you please suggest what is wrong with my script. I am opening each file turn by turn and pusing the value in hash. all the matching records are being stored together. Please help me with the modification in my script. Thanks
        The problem with your code is that you are loading all your files into memory (the %seen hash).

        What I am suggesting is to load only the first file into such a hash, and then to read line by line all the other files and, for each record, see if you have already seen that record in the first file. If you haven't seen it, you know that record will not be in all the files, since it wasn't in the first file anyway, so there is no point to add it to your hash. If you've seen it, just increment the value of the hash for it.

        At the end, your hash is in fact a set of counters telling, for each record of the first file, how many times it has been seen. You just need to keep the records whose counter is equal to the number of files.

        Update: please look at my other post in reply to your original question (at the bottom of the thread) for a detailed algorithm.