Greetings to all

With the help of perl monks I was able to get desired output as follows which is my new input now. I have abc.txt file which as 26 columns and 1072 rows with data as follows:

ID file1 file2 file3 file4 file5 ...file25 ID1 ABC12 ABC12 - ABC12 - ABC12 ID2 XYZ11 - - - XYZ11 - ID3 - - EFG21 EFG21 - EFG21 ...... ...... ID1072 - PQR34 PQR34 - PQR34 -

I want to read each row, print all IDs (first column) as such and print unique word in each row (say ABC12) and the times the word is repeated (4 in this case).

I split this problem into two codes and then thought of combining the two codes to get output

I printed first column using following code:

#!/usr/bin/perl use warnings; use strict; open (my $f, '<', 'abc.txt') or die; while (my $line = <$f>) { my @elems = split ' ', $line; print " ", $elems[0]; print "\n"; }

This code is working and it prints first column as desired output

Then, I removed first column from my data. With the remaining data (pm.txt), I tried following code and it does give me unique word and their counts but it prints only 1057 rows.

#!/usr/bin/perl use strict; use warnings; my %wordcount = (); open(theFile,"<pm.txt"); while(my $line = <theFile>) { chomp($line); my @words = split(' ', $line); foreach my $word(@words) { $wordcount{$word} += 1; } } foreach my $key(keys %wordcount) { print "Word: $key Repeat_Count: " . ($wordcount{$key} ) . "\n"; }

I thought of combining the two codes for solution, but it doesn't turned out what i expected. I want my output to be like this.

ID1 ABC12 4 ID2 XYZ11 2 ID3 EFG21 3 ...... ...... ID1072 PQR34 3

Thank you in advance


In reply to print first column, unique word in each row along with number times that word is repeated in each row by mao9856

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.