Perl_Noob2021 has asked for the wisdom of the Perl Monks concerning the following question:
$x="Room_reserve.csv"; $in = "D:\\package properties\\${x}.csv"; $in = "D:\\package properties\\${x}.csv"; $out = "D:\\package properties\\output\\${x}_output.csv"; open($fh, '<', $in) or die "Could not open file '$file' $!"; @data = <$fh>; close($fh); %counts; @columns; $first = 1; #counter foreach $dat (@data) { chomp($dat); @rows = split(',',$dat); if ($first == 1) { $first = 0; next; } else { $count = 1; foreach $i (0..$#rows) { if ( exists($columns[$i]{$rows[$i]}) ) { $columns[$i]{$rows[$i]}++; } else { $columns[$i]{$rows[$i]} = int($count); } } } } #output $first = 1; open($fh, '>', $out) or die "Could not open file '$file' $!"; foreach $dat (@data) { chomp($dat); @rows = split(',',$dat); foreach $i (0..$#rows) { if ($i > 6) { #for modifying name if ( $first == 1 ) { $line = join( ",", "Rank_$rows[$i]", "Percent_$rows[$i]", "C +ount_$rows[$i]", $rows[$i]); print $fh "$line,"; if ( $i == $#rows ) { $first = 0; } } else { @dat_val = reverse sort { $a <=> $b } values %{$columns[$i]} +; %ranks = {}; $rank_cnt = 0; foreach $val (@dat_val) { if ( ! exists($ranks{$val}) ) { $rank_cnt++; } $ranks{$val} = $rank_cnt; } $rank = $ranks{$columns[$i]{$rows[$i]}}; $cnt = $columns[$i]{$rows[$i]}; $ave = ($cnt / 14000000) * 100; $line = join( ",", $rank, $ave, $cnt, $rows[$i]); print $fh "$line,"; } } else { print $fh "$rows[$i],"; } } print $fh "\n"; } close($fh);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: efficient perl code to count, rank
by haj (Vicar) on Jul 17, 2021 at 18:05 UTC | |
by Perl_Noob2021 (Initiate) on Jul 17, 2021 at 18:36 UTC | |
by haj (Vicar) on Jul 17, 2021 at 20:16 UTC | |
|
Re: efficient perl code to count, rank
by LanX (Saint) on Jul 17, 2021 at 19:20 UTC | |
by haj (Vicar) on Jul 18, 2021 at 18:49 UTC | |
by LanX (Saint) on Jul 18, 2021 at 19:33 UTC | |
by Perl_Noob2021 (Initiate) on Jul 17, 2021 at 22:26 UTC | |
|
Re: efficient perl code to count, rank
by haj (Vicar) on Jul 17, 2021 at 23:00 UTC | |
|
Re: efficient perl code to count, rank
by tybalt89 (Monsignor) on Jul 18, 2021 at 18:03 UTC | |
by LanX (Saint) on Jul 18, 2021 at 19:28 UTC | |
by tybalt89 (Monsignor) on Jul 18, 2021 at 20:00 UTC | |
by LanX (Saint) on Jul 18, 2021 at 20:12 UTC | |
by karlgoethebier (Abbot) on Jul 19, 2021 at 06:11 UTC | |
|
Re: efficient perl code to count, rank
by roboticus (Chancellor) on Jul 17, 2021 at 18:23 UTC | |
by Perl_Noob2021 (Initiate) on Jul 17, 2021 at 18:48 UTC | |
|
Re: efficient perl code to count, rank
by Tanktalus (Canon) on Jul 17, 2021 at 20:11 UTC | |
by haj (Vicar) on Jul 17, 2021 at 20:35 UTC | |
by erix (Prior) on Jul 17, 2021 at 23:06 UTC | |
by haj (Vicar) on Jul 17, 2021 at 23:34 UTC | |
by Tanktalus (Canon) on Jul 18, 2021 at 21:39 UTC | |
by haj (Vicar) on Jul 18, 2021 at 22:52 UTC | |
by LanX (Saint) on Jul 18, 2021 at 23:17 UTC | |
| |
by LanX (Saint) on Jul 17, 2021 at 21:13 UTC | |
by LanX (Saint) on Jul 18, 2021 at 12:36 UTC | |
| |
|
Re: efficient perl code to count, rank
by eyepopslikeamosquito (Archbishop) on Jul 19, 2021 at 03:39 UTC | |
|
Re: efficient perl code to count, rank
by karlgoethebier (Abbot) on Jul 18, 2021 at 16:31 UTC | |
| A reply falls below the community's threshold of quality. You may see it by logging in. |