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. :-)


In reply to Hashes with Multiple Keys and Combining Them by de2425

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.