Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Ranking files, largest to smallest

by ewexperts (Initiate)
on May 27, 2008 at 05:18 UTC ( #688596=perlquestion: print w/ replies, xml ) Need Help??
ewexperts has asked for the wisdom of the Perl Monks concerning the following question:

Alright, I have a series of files that I need to access the first line in. They are all ending in .total (example: blackdeath.total and dandelion.total) and are all in the same directory.

What I'd like to do is have perl organize them by value, from largest to smallest. As a step to this, I've created a textfile with the filename for each one of these on a seperate line.

The Text File:

Black Death Jesse Gunn The Crimson Shadow Ariana London Legion Scorpio Greg Manix


etc

Now, I'm not exactly sure how to do this, but this is what I have so far:

#!/usr/local/bin/perl use strict; use warnings; use CGI qw/:standard/; use CGI::Carp qw/fatalsToBrowser/; my $wrestlerdir = '/home/ewexpert/public_html/rosterrank'; open(FILE, "$wrestlerdir/wrestlerlist.txt"); my @lines = <FILE>; chop(@lines); foreach my $str (@lines) { chomp(@lines); } close(FILE); foreach my $test (@lines) { open my $fh, "<", "$wrestlerdir/@lines.total" or die "Can't open @line +s.total $!"; my @total = <$fh>; } print header, start_html('Rankings: '), end_html;




Well, it's a start. Unfortunately, it doesn't work and I think part of it is that chomp is the wrong command. Here's what it's displaying:



Can't open Black Death Jesse Gunn The Crimson Shadow Ariana London Legion Scorpio Greg Mani.total No such file or directory at test.cgi line 21.

Comment on Ranking files, largest to smallest
Select or Download Code
Re: Ranking files, largest to smallest
by ikegami (Pope) on May 27, 2008 at 05:30 UTC

    I think part of it is that chomp is the wrong command

    That's not it. You use the whole array to build the file name instead of just one element of it.
    open my $fh, "<", "$wrestlerdir/@lines.total"
    should be
    open my $fh, "<", "$wrestlerdir/$test.total"

    Unrelated,
    chop(@lines); foreach my $str (@lines) { chomp(@lines); }
    should be
    chomp(@lines);

Re: Ranking files, largest to smallest
by prasadbabu (Prior) on May 27, 2008 at 05:31 UTC

    Hi ewexperts,

    Change

    my @lines = <FILE>; chop(@lines); foreach my $str (@lines) { chomp(@lines); }
    into chomp (@lines = <FILE>);

    Here chop is not necessary to remove the newline character, it ll remove the last character, not only newline character. Instead you can use chomp which removes only the newline character at the end.

    Prasad

Re: Ranking files, largest to smallest
by ewexperts (Initiate) on May 27, 2008 at 06:05 UTC
    Okay, I got it to stop giving me errors at least, using the following code:
    #!/usr/local/bin/perl use strict; use warnings; use CGI qw/:standard/; use CGI::Carp qw/fatalsToBrowser/; my $wrestlerdir = '/home/ewexpert/public_html/rosterrank'; open(FILE, "$wrestlerdir/wrestlerlist.txt"); my @lines = <FILE>; chomp (@lines = <FILE>); close(FILE); foreach my $test (@lines) { open my $fh, "<", "$wrestlerdir/$test.total" or die "Can't open @lines +.total $!"; my @total = <$fh>; } print header, start_html('Rankings: '), (@lines), end_html;
    However, from what I can tell I don't think it's working. It's just outputting a blank page. Even more troublesome is that I know that 1 or 2 of the .total files aren't actually there, because I wanted to see what it would do if the files weren't present. It should print "Can't open @lines.total" but it's just entirely blank.

      example: blackdeath.total and dandelion.total

      The Text File: Black Death #check the space Jesse Gunn The Crimson Shadow Ariana London Legion Scorpio Greg Manix

      I doubt that in the file where you are reading the file names is having space and in the file names which you have given in example doesn't have space. So you check that.

      Also In the below code, first line is not necessary.
      change

      my @lines = <FILE>; chomp (@lines = <FILE>);
      into

      chomp (@lines = <FILE>);

      Prasad

      Given the assumption of homework...

      #!/usr/local/bin/perl use strict; use warnings; use CGI qw/:standard/; use CGI::Carp qw/fatalsToBrowser/;

      Strict/warnings: good habit to start with. Also, good to use "standard", tested modules (CGI, CGI::Carp) rather than re-inventing wheel.

      my $wrestlerdir = '/home/ewexpert/public_html/rosterrank'; open(FILE, "$wrestlerdir/wrestlerlist.txt"); my @lines = <FILE>; chomp (@lines = <FILE>); close(FILE);

      As mentioned by the previous poster, the first @lines=<FILE> line is slurping all of your data, and then being overwritten. Use the second line. Additionally, the indenting between the open and close is a little misleading. I understand why you did it, but the structure implies that @lines may not be in scope after the close(), especially since it is being declared with my.

      foreach my $test (@lines) { open my $fh, "<", "$wrestlerdir/$test.total" or die "Can't open @lines +.total $!"; my @total = <$fh>; }
      • The variable @total will be re-initialized for each file, and will go out of scope at the end of the loop
      • Good use of the "3 argument version" of open. Safer than the form open my $fh, "<$data.txt"
      • @total is slurping in the entire contents of the file. From your description, it sounds as if you only need to read the first line. Search this site for the difference between scalar and list context while doing a read.
      • @total is not used anywhere other than to read the data from the file. See the first comment in this section. I am guessing it may help in this area :)
      print header, start_html('Rankings: '), (@lines), end_html;

      This is using "Rankings: " as the page title, and nothing more. You are then printing all of the contents of the list of files, without any HTML formatting (at least in this version of your program), and then the HTML terminator. See the previous set of comments, and the last respondent. I think they may go straight to the heart of the issues in your solution.

      Good luck, and ask if you have any more questions on this.

      --MidLifeXis

Re: Ranking files, largest to smallest
by andreas1234567 (Vicar) on May 27, 2008 at 06:19 UTC
    Read readdir, -X, sort, map.
    $ perl -wl use strict; my $dir = q{.}; opendir(DH, $dir) || die "can't opendir $dir: $!"; my %filesize = map { -s "$dir/$_" => $_ } readdir(DH); closedir DH; print "$filesize{$_} : $_" for(sort {$b <=> $a} keys %filesize); __END__
    --
    No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]
Re: Ranking files, largest to smallest
by mhearse (Hermit) on May 27, 2008 at 08:47 UTC
    As far as getting the file size: I recommend the stat function. Or, -s as has already been shown.
Re: Ranking files, largest to smallest
by MidLifeXis (Prior) on May 27, 2008 at 10:54 UTC

    Is this, perchance, homework? The problem sounds very manufactured.

    If it is homework, that is not a problem. Just tell us. We are willing to help. You have shown what you have done already, which is good.

    BTW: welcome to the monastary.

    --MidLifeXis

      Thanks for the input, guys. It's actually not homework, I'll explain further. And in fact, I may be way out of my league here in trying to do this.

      I run a fantasy-wrestling website, of about 400-500 wrestlers, and until now I've been ranking them in an excel file, then manually grabbing the top 20 ranked wrestlers and placing them in a text file, to include on the front page. You can see it at http://www.ewexperts.com on the right side.

      The great idea I had was to implement a script-based system, where my staff members can monitor matches and then submit updated stats, for me to review and either confirm or reject (to prevent cheating).

      From this script, I would like the wrestler ranked (not by file-size as I think some people interpreted) but by largest-to-smallest based on the number that exists in each .total file.

      I have the first script created already (the one where the stats are submitted into different files, example: (Jason Blade.wins, Jason Blade.losses, Jason Blade.total)

      But, I think I might be out of my realm of capability on this one, because nothing I try seems to be working and so I'll probably look to hiring out for this one.

      -Jesse
      ewexperts@tmo.blackberry.net

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://688596]
Approved by prasadbabu
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (10)
As of 2014-08-30 17:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls