arrays - Compare two CSV Files with Perl -


I have two CSV files that I want to compare with Pearl.

I have code using files in Perl and it gives me a nice array of hash references for files.

using all of my data import shows correctly.

  Use strict; Use warnings; Use text: CSV :: slurp; Use Data :: Dumper :: Short; My $ file1_src = "ipb-csv.csv"; My $ file2_src = "SRM-CSV.csv"; My $ ipb = text :: csv :: slur-> load (file = & gt; $ file1_src); My $ srm = text :: csv :: slur-> load (file = & gt; $ file2_src); Print dumper ($ ipb); Print dumper ($ SRM);  

The result of the dump looks like this

$ ipb

  [[drawing => gt; ; "1001"}, {drawing = & gt; "1002"}, {drawing = & gt; "1003"}]  

$ SRM

  [[Drawing = & gt; "1001", Figure => "Figure 2-8", index = & gt; 2, naming = & gt; "Some Parts"}, {Drawing = & gt; "1002", Figure => "Figure 2-8", index = & gt; 2, naming = & gt; "Some Parts"}, {Drawing = & gt; "2001", Figure => "Figure 2-8", index = & gt; 2, naming = & gt; "Some Parts"}, {Drawing = & gt; "2002", Figure => "Figure 2-8", index = & gt; 2, naming = & gt; "Some Parts"}]  

I want to compare two arrays based on the drawing key of each hash, and make two CSV files as follows

  • An item that contains $ IPB but is not $ SRM , which contains data only in 'Drawing Columns'.

  • Another item where $ SRM is in, but is not $ IPB , which contains all the code that is < Code related to> Drawing column

  • I have got a lot of information to see the file, whether they meet, or compare the hash or arrays for single pieces of data, but I can not find anything special for this.

This short program uses your example values ​​ $ ipb and < Code> $ srm and makes the output that I think you want please do not use capital letters for anything but global identifiers, like the package names.) < / P>

There are some problems

  • Uses, without any indexing, no use has been done for this task. Leaves with it. You will be better off creating scratches with appropriate data structures by processing file line-by-line

  • You say that your second file is related to each All notifications must be Drawing key, but because Pearl hashes are naturally unarmed, text :: CSV :: slurp has lost the order of field names. The best that can be done is to print whatever data it is getting in, but it is before the header line displaying the fields' names. This is another reason to avoid Text :: CSV :: Slurp

 ; Strict; Use warnings; Use Autodi; # Basic Data My $ ipb = [{drawing = & gt; 1001}, {drawing = & gt; 1002}, {drawing = & gt; 1003}]; My $ srm = [{Drawing = & gt; "1001", Figure => "Figure 2-8", index = & gt; 2, naming = & gt; "Some Parts"}, {Drawing = & gt; "1002", Figure => "Figure 2-8", index = & gt; 2, naming = & gt; "Some Parts"}, {Drawing = & gt; "2001", Figure => "Figure 2-8", index = & gt; 2, naming = & gt; "Some Parts"}, {Drawing = & gt; "2002", Figure => "Figure 2-8", index = & gt; 2, naming = & gt; "some part"   } ]; # Index my% srm; For my $ item (@ $ srm) {my $ drawing = $ item- & gt; {Drawing}; $ Srm {$ drawing} = $ item; } My% ipb; For $ my $ item (@ $ ipb) {my $ drawing = $ item- & gt; {Drawing}; $ Ipb {$ drawing} = 1; } # Output files open my $ csv1, '& gt;', 'file1.csv'; For my $ ID (sort key% ipb) {next $ $ srm {$ id}; Print $ csv1 $ id, "\ n"; } About $ csv1; Open my $ csv2, '& gt;', 'file2.csv'; My @ keys = keys% {$ srm-> [0]}; Join print $ csv2 (',', @keys), "\ n"; For next $ ipb {$ id} for my $ id (sort keys% srm); Print included $ csv2 (',', @ {$ srm {$ id}} {@ key}}, "\ n"; } Close $ csv2;  

Output

file1.csv

  1003  

file2.csv

  drawing, naming, index, figure 2001, part 2, 2, Figure 2-8 2002, some parts , 2, Figure 2-8,  

Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -