in reply to How to combine these two files?

Can someone show me some head-ups on how to generate an output like this?
#!/usr/bin/perl use strict; use warnings; print <<'__xxx__'; Item Availability Broken Newly_order Total_Stock Laptop_Monitor 10 2 15 (10-2+15) Laptop_Bag 25 1 20 (25-1+20) Office_Chair 20 1 10 (20-1+10) __xxx__

Or did you want to generate that output from any input?

Perlmonks is no code writing service. Show your code, then we'll help you improve it.

I am new to Perl and this is the first assignment that I get.

Well, start with perlintro and the notes you made before the assignment.

How would you solve this problem in another language, like C or Java? Write that down, then convert it to Perl.

If you are new to programming, imagine you had an incredible stupid, but diligent assistent. How would you explain the job to him, in baby steps and simple language? ("Open the first file. Read a line from the first file. ...") From there, translating to Perl, C, Java or most other computer languages is quite easy.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Replies are listed 'Best First'.
Re^2: How to combine these two files?
by chaney123 (Acolyte) on Aug 29, 2017 at 07:48 UTC

    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?

      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.

      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.