Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Since you didn't provide a small test data set I had to guess about it and then fake it (I may have guessed wrong).

You were doing the ranking sort for each column for each line when it only had to be done once per column.

I am reading through the file twice so that the whole file does not have to be stored in memory. Because of that I had to change your file handle names.

I had to change your I/O because a) I'm not on windows, and b) debug. I think I labeled all those changes with FIXME.

Of course, comment out the Data::Dump before running on a large file. You'll find Data::Dump is extremely slow on huge data structures.

As I've said to others here, at least my code does not fail any of your provided test cases :)

#!/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 = <<END; # FIXME 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 END $in = \$testdata; #FIXME open(my $infh, '<', $in) or die "Could not open file '$in' $!"; my @columns; my $lines = 0; <$infh>; # 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', \@ranks +bycolumn; # FIXME my $first = 1; #output #open(my $outfh, '>', $out) or die "Could not open file '$out' $!"; #F +IXME open(my $outfh, ">&STDOUT") or die "Could not open file STDOUT$!"; #FI +XME 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;

Outputs:

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,

In reply to Re: efficient perl code to count, rank by tybalt89
in thread efficient perl code to count, rank by Perl_Noob2021

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2021-12-09 09:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (36 votes). Check out past polls.

    Notices?