de2425 has asked for the wisdom of the Perl Monks concerning the following question:
I'm sorry to bother everyone again but you've all been so helpful. I hope you don't mind my asking for help again.
I am having some difficulty thinking of a way to do something that I'm sure is simple. I, however, just cannot get my brain around a way to get Perl to do it. What I have is a file with multiple records. The records contain an ID, name, description, application in that order. There are in some cases several of the same ID listed because one ID may have more than one application. What I want to do is print out the same data to anohter file but I want the ID's listed only once and all of the applications listed in the one line. So basically the data looks like this:
ID, name, description, application
1, XYZ, desc, PDF
1, XYZ, desc, QFZ
2, YGH, desc, LMN
What I'm looking for is:
1, XYZ, desc, PDF,QFZ
2, YGH, desc, LMN
The code I have been working on is pasted below. I am getting results but I am getting duplicate values in the last column and cannot figure out how to get them to print different values for one ID on the same line.
#!/usr/bin/perl -w open (IN, "c:/work/Abnova_Grouping/Abnova_App_and_Key.txt"); while(<IN>){ chomp; @t=split(/\t/,$_); $AB{$t[0]}.= ",$t[2]"; $AB{$t[0]}=$t[2].",$t[2]"; } close IN; open (OUT, ">c:/work/Abnova_Grouping/Abnova_Products.txt"); open (IN, "c:/work/Abnova_Grouping/Abnova_Product_List.txt"); while (<IN>){ chomp; @Products=split(/\t/,$_); if ($Products[0]=~/\d/ and exists $AB{$Products[0]}){ print OUT "$Products[0]\t$Products[2]\t$AB{$Products[0]}\n" +; } } close IN; close OUT;
I have been trying to think of a good way to do this for a while and haven't come up with anything. If anyone could help, I'd very much appreciate it.
P.S. I do know what I've been told about using the "my" before the various scalars, arrays, hashes, etc. The person that I'm writting these for doesn't like when I use those. That is why I don't. Please don't think me beligerant for not using them.
Thank you all once again for helping to bail me out of a spot. I finally got it going and I appreciate all of your help. Here is the code that I finally got to generate the output that I needed:
#!/usr/bin/perl -w open (IN, "c:/work/Abnova_Grouping/Abnova_App_and_Key.txt"); while(<IN>){ chomp; @t=split(/\t/,$_); $AB{$t[0]}=$AB{$t[0]}.",$t[2]"; } print %AB; close IN; close OUT; open (OUT, ">c:/work/Abnova_Grouping/Abnova_Products.txt"); open (IN, "c:/work/Abnova_Grouping/Abnova_Product_List.txt"); while (<IN>){ chomp; @Products=split(/\t/,$_); if ($Products[0] and exists $AB{$Products[0]}){ print OUT "$Products[0]\t$Products[2]\t$AB{$Products[0]}\n" +; } } close IN; close OUT;
Thank you all again so much!! I only hope to get to the point where I can be of some assistance to you. :-)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Hashes with Multiple Keys and Combining Them
by johngg (Canon) on Sep 10, 2008 at 20:16 UTC | |
|
Re: Hashes with Multiple Keys and Combining Them
by toolic (Bishop) on Sep 10, 2008 at 19:03 UTC | |
by de2425 (Sexton) on Sep 10, 2008 at 19:55 UTC | |
|
Re: Hashes with Multiple Keys and Combining Them
by Tanktalus (Canon) on Sep 11, 2008 at 02:19 UTC | |
|
Re: Hashes with Multiple Keys and Combining Them
by didess (Sexton) on Sep 10, 2008 at 21:10 UTC | |
|
Re: Hashes with Multiple Keys and Combining Them
by milarepa (Beadle) on Sep 10, 2008 at 18:46 UTC |