mao9856 has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: print first column, unique word in each row along with number times that word is repeated in each row
by Eily (Monsignor) on Jan 10, 2018 at 10:30 UTC | |
by mao9856 (Sexton) on Jan 11, 2018 at 03:19 UTC | |
by poj (Abbot) on Jan 11, 2018 at 06:37 UTC | |
by mao9856 (Sexton) on Jan 11, 2018 at 07:11 UTC | |
|
Re: print first column, unique word in each row along with number times that word is repeated in each row
by BillKSmith (Monsignor) on Jan 10, 2018 at 13:35 UTC |