Forum Moderators: coopster & phranque

Message Too Old, No Replies

Re: Three array problem

I need some help. Desperately!

         

adni18

1:20 am on Sep 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's my dilemma:

@wallpaperone=("Quick Goo","17 Votes","6.792");
@wallpapertwo=("Cool Planet","25 Votes","3.6");

is there a way I can sort these by the third value(Average Rating) in the array, then print something like this:

1. Quick Goo
17 Votes
Rated 6.792

2. Cool Planet
25 Votes
Rated 3.6

The only problem is that I can't order @wallpaperone and @wallpaper two then retrieve the second and third values, because they might change. I know I have severely underexplained this, so here it is in a nutshell:
I have twenty wallpapers, all of which have results stored in their own independent text files. They look like this when read: Palanet¦134¦7.5 . I need to sort them all by the last value (7.5 in this case), then display the top ten, along with not only each of the individual wallpaper's rating, but along with its name and number of votes, too. Thanks a whole lot in advance.

moltar

3:42 am on Sep 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[perl]
#!/usr/bin/perl

use strict;

my $WP = 'wp_data.txt';


open WP, "< $WP" or die "Cannot open $WP: $!";
chomp ( my @wp_data = <WP> );
close WP;

my @wp_data_sorted = sort {
(split '\¦', $b, 3)[2] <=>
(split '\¦', $a, 3)[2]
} @wp_data;

print "Content-type: text/html\n\n";

for ( my $i=0; $i<10; $i++ ) {
my ($name, $votes, $rating) =
split '\¦', $wp_data_sorted[$i], 3;

print "$name, $votes, $rating\n";
}
[/perl]

Make sure to change ¦ to actual pipe symbols. Forum software, automatically replaces them to the broken pipe symbol.

adni18

11:07 am on Sep 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok so this script:
opens one file (I can open more, right?)
sorts them by average rating
and displays just the top ten.

Right?

moltar

2:16 pm on Sep 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can open more, but it will be a slightly different script.

adni18

8:32 pm on Sep 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, can you compile that for me? Cause I stink at Perl.

coopster

3:00 pm on Sep 25, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



adni18,

You need to at least give it your best effort first as these forums are designed to help you learn perl, not have somebody else do it for you. Please have a look at the Charter [webmasterworld.com] for posting guidelines.

adni18

3:49 pm on Sep 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're right. I am sorry. The only problem is, I can't test my revised version of the script because my server is down [webmasterworld.com] (No one responded)

adni18

10:03 pm on Sep 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay, now believe me when I say that I just tried to edit the script several different ways, all returning various errors. Could someone at least explain these portions of the code?:

my @wp_data_sorted = sort {
(split '\¦', $b, 3)[2] <=>
(split '\¦', $a, 3)[2]
} @wp_data;

and


for ( my $i=0; $i<10; $i++ ) {
my ($name, $votes, $rating) =
split '\¦', $wp_data_sorted[$i], 3;

adni18

10:25 pm on Sep 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nevermind. I figured it out by myself, however I would like to apologize for my breaking of the charter.

Thank you so much, Moltar.