john.tm has asked for the wisdom of the Perl Monks concerning the following question:
The code i have so far. I can keep only unique elements but am stuck on how to show duplicate count.input output james james 2 dave dave 2 mike mike 3 ken ken 3 jon jon 5 jon ken jon mike james dave mike ken jon jon
#!/usr/bin/perl use strict; use warnings; use Tk; use Tk::BrowseEntry; use POSIX 'mktime'; use POSIX 'strftime'; open(STDERR, ">&STDOUT"); ######## entry widget to get $yyyy $mmm $dd #################### +################### print "\n Select Year = $yyyy\n"; print "\n Select Month = $mmm\n"; print "\n Number of Backup Days = $dd\n"; ######## create input and output files ################## +##################### my $filerror = "\n\n! Cannot open File below, please check it exists o +r is not open already?\n"; my $OUTFILE = "C:\\Temp\\$yyyy\$mmmAudit.txt"; my $INFILE1 = "c:\\$yyyy\\$mmm\\report.csv"; my $INFILE = "c:\\$yyyy\\$mmm\\names.csv"; #Open input file for reading and Output file for writting open (INPUT,"$INFILE") or die "\n$filerror\$INFILE",,1; #open (OUTPUT,">$OUTFILE") or die "\n$filerror\n$OUTFILE",,1; my $total_names = 0; $total_names++ while (<INPUT>); my $Month_total = $total_names * $dd; ######### get total number of rows in files ################# +################# print "\n Total number of names is $total_names\n"; print "\n Total number of names is $Month_total\n"; close INPUT; open (INPUT,"$INFILE") or die "\n$filerror\$INFILE",,1; ######### keep only unique names and display number count of du +plactes ######### my %seen; while (<INPUT>) { chomp; my $line = $_; my @elements = split (",", $line); my $col_name = $elements[1]; print " $col_name \n" if ! $seen{$col_name}++; } close INPUT;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: keep only unique elements in an array displaying number of duplcates.
by GotToBTru (Prior) on Jul 28, 2014 at 04:25 UTC | |
|
Re: keep only unique elements in an array displaying number of duplcates.
by 2teez (Vicar) on Jul 28, 2014 at 06:38 UTC | |
|
Re: keep only unique elements in an array displaying number of duplcates.
by Anonymous Monk on Jul 28, 2014 at 04:18 UTC | |
by john.tm (Scribe) on Jul 28, 2014 at 04:55 UTC | |
by Anonymous Monk on Jul 28, 2014 at 07:04 UTC |