in reply to Re: How to combine these two files?
in thread How to combine these two files?

Hi,

Actually I have never learn any programming language before. I have just recently enrolled into my school and this is the first subject that I am taking. The question that i posted is actually the 2nd part that the lecturer request us to do. For the first part, he ask us to convert the .txt files into csv files which I had already completed. However I had no idea how to extract the datas that are in file1 and file 2 into a new file since the number of lines in both files are different. Should I use pattern matching? Or what method should I use?

Replies are listed 'Best First'.
Re^3: How to combine these two files?
by Corion (Patriarch) on Aug 29, 2017 at 08:14 UTC

    Ideally your lecturer would have covered a data structure to use in the course material. It would be rare that the exercises do not match up with the material the lecturer has covered.

    Maybe if your course notes are incomplete, talk to other members of the course.

    There are many approaches to linking data from two lists together. The most common approach is to take the shorter list and put its elements into a hash. Then, for each element in the longer list you take the key of the element and look that up in the hash.

Re^3: How to combine these two files?
by Laurent_R (Canon) on Aug 29, 2017 at 08:01 UTC
    Hi, there are many possible ways to do that, but the simplest would be to read one of the files into a hash (for example with the item as a key and the rest of the line as a value), and then to read the other file and construct your output from each line you read from the second file together with what you have stored into the hash from the first file. This assumes, of course, that you've learned to use hashes. An alternative might be to sort both files and read them in parallel, but this is likely to be more difficult.