Majority Voting in perl?

Posted by aliocee on Stack Overflow See other posts from Stack Overflow or by aliocee
Published on 2010-12-29T23:17:48Z Indexed on 2010/12/29 23:54 UTC
Read the original article Hit count: 207

Filed under:

Hi,

i have 5 files containing the same words, i want read each word in all files and decide the winning word by detecting the following characters in a word (*, #, $, &) and generate output file i can only have 2 winners for example:

file1

    we$
    are*
    ...

file2

    we$
    are*
    ...

file3

    we*
    are$
    ...

file4

    we$
    are$
    ...

file5

    we#
    are&
    ...

output file:

we$             - we$ is the winner since it occur in 3 files.          
are*$           - are* and are$ are the winners since both occur 2 times.

here is how i started:

#!/usr/local/bin/perl -w

sub read_file_line {

  my $fh = shift;    
  if ($fh and my $line = <$fh>) {    
    chomp($line);    
    return $line;

  }    
  return;    
}

open(my $f1, "words1.txt") or die "Can't";
open(my $f2, "words2.txt") or die "Can't";
open(my $f3, "words3.txt") or die "Can't";
open(my $f4, "words4.txt") or die "Can't";
open(my $f5, "words5.txt") or die "Can't";


my $r1 = read_file_line($f1);
my $r2 = read_file_line($f2);
my $r3 = read_file_line($f3);
my $r4 = read_file_line($f4);
my $r5 = read_file_line($f5);

while ($f5) {



    what can i do here to decide and write the winning word in the output file?



$r1 = read_file_line($f1);
$r2 = read_file_line($f2);
$r3 = read_file_line($f3);
$r4 = read_file_line($f4);
$r5 = read_file_line($f5);
}

Thanks.

© Stack Overflow or respective owner

Related posts about perl