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

Hi all,

So these are the example files that I have. I actually have thousand of lines of data in each files. To make it easier to read, I shorten it.

File_1 Item Availability Broken Laptop_Monitor 10 2 Laptop_Case 20 0 Laptop_Bag 25 1 Office_Chair 20 1 Mouse 10 0 Keyboard 10 0 File_2 Item Newly_order Ref_Name Laptop_Monitor 15 HMX-190B-F Laptop_Bag 20 BMT-1920-U Office_Chair 10 ORD-455_T

So From these the two files this is the output that I wish to have:

outfile_1 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)
Can someone show me some head-ups on how to generate an output like this? I am new to Perl and this is the first assignment that I get.

Replies are listed 'Best First'.
Re: How to combine these two files?
by afoken (Chancellor) on Aug 29, 2017 at 06:09 UTC
    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". ;-)

      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.
Re: How to combine these two files?
by thanos1983 (Parson) on Aug 29, 2017 at 08:16 UTC

    Hello chaney123,

    Fellow Monk afoken is absolutely right. You need to work on the solution to your problem in your mind first and then put it down to paper. The task is fairly easy even for a beginner. It is not about us providing you with the solution it is about you learning on the process by trying and failing, again and again.

    Having said that, there are multiple ways of resolving your problem. One possible way that is as much simple as possible could be:

    Step 1: open both files in reading mode (I assume you know how to do that).

    Step 2: read the files and place them into arrays, see here how to do that on a relevant question How to populate array with lines from text file? (Moved from Q&A) (close files after putting the data into the arrays).

    Step 3: iterate on both arrays (so you can read one line at a time simultaneously). Sample of code on similar question How do I Loop over multiple arrays simultaneously ?. Update: (I just noticed that both files are not equal). So one possible way is to check if the element exist in the array if not, apply next. Experiment and see what are your problems and try to resolve them.

    Step 4: Do what ever modification on the lines append to each other based on the data that you have.

    Step 5: open a file in writing mode, export the data and you are done. :D

    This is a high level implementation, there is a very small tricky point that you need to resolve regarding the data processing. You need to resolve this on your own, remember it is all about trying and failing. If you encounter any problems post your code, tell us where you failed and we will assist.

    Hope this gave you a direction of proceeding, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: How to combine these two files?
by tybalt89 (Monsignor) on Aug 29, 2017 at 19:54 UTC
    #!/usr/bin/perl # http://perlmonks.org/?node_id=1198243 use strict; use warnings; open my $one, '<', \<<END or die; # replace with normal open Item Availability Broken Laptop_Monitor 10 2 Laptop_Case 20 0 Laptop_Bag 25 1 Office_Chair 20 1 Mouse 10 0 Keyboard 10 0 END open my $two, '<', \<<END or die; # replace with normal open Item Newly_order Ref_Name Laptop_Monitor 15 HMX-190B-F Laptop_Bag 20 BMT-1920-U Office_Chair 10 ORD-455_T END $_ = join '', <$one>, <$two>; print "Item Availability Broken Newly_order Total_ +Stock\n"; printf "%-16s%7d%12d%11d%22s\n", $1, $2, $3, $4, "($2-$3+$4)" while /^(\S+)\s+(\d+)\s+(\d+)(?=.*^\1\s+(\d+))/gms;

    Outputs:

    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)
      #!/usr/bin/perl # http://perlmonks.org/?node_id=1198243 use strict; use warnings; open my $one, '<', \<<END or die; # replace with normal open Item Availability Broken Laptop_Monitor 10 2 Laptop_Case 20 0 Laptop_Bag 25 1 Office_Chair 20 1 Mouse 10 0 Keyboard 10 0 END open my $two, '<', \<<END or die; # replace with normal open Item Newly_order Ref_Name Laptop_Monitor 15 HMX-190B-F Laptop_Bag 20 BMT-1920-U Office_Chair 10 ORD-455_T END $_ = join '', <$one>, <$two>; print "Item Availability Broken Newly_order Total_ +Stock\n"; printf "%-16s%7d%12d%11d%22s\n", $1, $2, $3, $4, "($2-$3+$4)" while /^(\S+)\s+(\d+)\s+(\d+)(?=.*^\1\s+(\d+))/gms;

      This is not helpful. Absolutely not helpful. Yes, this mess might produce the expected result. But that line noise at the end is incomprehensible for a beginner. And to make things worse, that join/rexexp/printf hack is probably as far away from the explained and expected solution as possible. Nothing to learn here for a beginner.

      If you were my student, you would fail the test. No comments, no documentation, instant fail. No proper error handling, again instant fail. It's that simple. Yes, you would get a bonus point for creative abuse of regular expressions, but you would still fail.

      Alexander

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