#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11135101 use warnings; use List::Util qw( uniq ); my $x="Room_reserve.csv"; my $in = "D:\\package properties\\${x}.csv"; $in = "D:\\package properties\\${x}.csv"; # ????????? duplicate line??? my $out = "D:\\package properties\\output\\${x}_output.csv"; print my $testdata = <; # skip first line while( <$infh> ) { my @rows = split /[,\n]/; $columns[$_]{$rows[$_]}++ for 0..$#rows; $lines++; } my @ranksbycolumn; for ( @columns ) { my @dat_val = sort { $b <=> $a } uniq values %$_; my %ranks; @ranks{ @dat_val } = 1 .. @dat_val; push @ranksbycolumn, \%ranks; } use Data::Dump 'dd'; dd 'columns', \@columns, 'ranksbycolumn', \@ranksbycolumn; # FIXME my $first = 1; #output #open(my $outfh, '>', $out) or die "Could not open file '$out' $!"; #FIXME open(my $outfh, ">&STDOUT") or die "Could not open file STDOUT$!"; #FIXME seek $infh, 0, 0 or die "Could not seek file '$in' $!"; while( <$infh> ) { my @rows = split /[,\n]/; foreach my $i (0..$#rows) { if ($i > 6) { if ( $first == 1 ) # for modifying name { my $line = join( ",", "Rank_$rows[$i]", "Percent_$rows[$i]", "Count_$rows[$i]", $rows[$i]); print $outfh "$line,"; if ( $i == $#rows ) { $first = 0; } } else { my $cnt = $columns[$i]{$rows[$i]}; my $rank = $ranksbycolumn[$i]{$cnt}; # my $ave = ($cnt / 14000000) * 100; # FIXME my $ave = int +($cnt / $lines) * 100; # FIXME my $line = join( ",", $rank, $ave, $cnt, $rows[$i]); print $outfh "$line,"; } } else { print $outfh "$rows[$i],"; } } print $outfh "\n"; } close $outfh; #### x,x,x,x,x,x,x,one,two,three x,x,x,x,x,x,x,foo,bar,baz x,x,x,x,x,x,x,foo2,bar7,baz3 x,x,x,x,x,x,x,foo2,bar7,baz3 x,x,x,x,x,x,x,foo,bax,baz x,x,x,x,x,x,x,foo,bar,baz x,x,x,x,x,x,x,foo,bar,baz ( "columns", [ { x => 6 }, { x => 6 }, { x => 6 }, { x => 6 }, { x => 6 }, { x => 6 }, { x => 6 }, { foo => 4, foo2 => 2 }, { bar => 3, bar7 => 2, bax => 1 }, { baz => 4, baz3 => 2 }, ], "ranksbycolumn", [ { 6 => 1 }, { 6 => 1 }, { 6 => 1 }, { 6 => 1 }, { 6 => 1 }, { 6 => 1 }, { 6 => 1 }, { 2 => 2, 4 => 1 }, { 1 => 3, 2 => 2, 3 => 1 }, { 2 => 2, 4 => 1 }, ], ) x,x,x,x,x,x,x,Rank_one,Percent_one,Count_one,one,Rank_two,Percent_two,Count_two,two,Rank_three,Percent_three,Count_three,three, x,x,x,x,x,x,x,1,66,4,foo,1,50,3,bar,1,66,4,baz, x,x,x,x,x,x,x,2,33,2,foo2,2,33,2,bar7,2,33,2,baz3, x,x,x,x,x,x,x,2,33,2,foo2,2,33,2,bar7,2,33,2,baz3, x,x,x,x,x,x,x,1,66,4,foo,3,16,1,bax,1,66,4,baz, x,x,x,x,x,x,x,1,66,4,foo,1,50,3,bar,1,66,4,baz, x,x,x,x,x,x,x,1,66,4,foo,1,50,3,bar,1,66,4,baz,