Gossamer Forum
Home : General : Perl Programming :

Dumb question

Quote Reply
Dumb question
How can i make a top 10 get data from a txt file ?

data file like:

name|email|votes

asd|asd@hastr.com|12
lero|lero@t.com|1
ze|ze@bah.com|10
re|re@irgh.com|3
aham|aham@oi.com|4
ui|ui@hi.com|9
asd|asd@hastr.com|12
lero|lero@t.com|1
ze|ze@bah.com|10
re|re@irgh.com|3
aham|aham@oi.com|4
ui|ui@hi.com|9
asd|asd@hastr.com|12
lero|lero@t.com|1
ze|ze@bah.com|10
re|re@irgh.com|3
aham|aham@oi.com|4
ui|ui@hi.com|9


i need to make a top 10 from this, order by 3 info, numbers. order results by higest number ...

I know the logic but i can't past to the note block =/


Thanks if any1 help me =D
and sorry for another post but its other subject.
oh and about my bad english too =D

Last edited by:

NamedRisk: Nov 8, 2003, 7:38 PM
Quote Reply
Re: [NamedRisk] Dumb question In reply to
Something like this will work:
Code:
#!/usr/bin/perl -w

use strict;

# Extract your data into a hashref
my $data = {};
while (<DATA>) {
chomp;
my ($name, $email, $votes) = split /\|/;
$data->{$name}->{votes} = $votes;
$data->{$name}->{email} = $email;
}

# Sort the data first by number of votes then alphabetically
my @sorted = sort {
$data->{$b}->{votes} <=> $data->{$a}->{votes} ||
$a cmp $b
} keys %$data;

print "Content-Type: Text/HTML\n\n";
print "<pre>\nTop Ten\n----------\n";

# Loop through the top ten
foreach (0 .. 9) {
print "$sorted[$_] ($data->{$sorted[$_]}->{email})" .
" has $data->{$sorted[$_]}->{votes} votes\n";
}

# I made all the names unique. If all your names are not
# unique, you will have to come up with some sort of unique
# identifier to key off of. Maybe something like:
# ID|Name|Email|Votes instead.

__DATA__
asd|asd@hastr.com|12
lero|lero@t.com|1
ze|ze@bah.com|10
re|re@irgh.com|3
aham|aham@oi.com|4
ui|ui@hi.com|9
asd1|asd@hastr.com|12
lero1|lero@t.com|1
ze1|ze@bah.com|10
re1|re@irgh.com|3
aham1|aham@oi.com|4
ui1|ui@hi.com|9
asd2|asd@hastr.com|12
lero2|lero@t.com|1
ze2|ze@bah.com|10
re2|re@irgh.com|3
aham2|aham@oi.com|4
ui2|ui@hi.com|9

The only problem I see is that your data doesn't have a unique key. You would have to make sure all the names are unique or come up with a unique ID system for each entry into the file. See my comments in the script.

~Charlie
Quote Reply
Re: [Chaz] Dumb question In reply to
thanks a lot man...

i understand what you do...

i only don't have the power to write this..

but now i can turn my self huahua



thanks brow =D
Quote Reply
Re: [NamedRisk] Dumb question In reply to
another dumb question LoL

it's about the code up here ...

why codes dont work inside foreach (0 .. 9 ) ?

like.

$n = 0;
foreach ( 0 .. 9 ) { $n++;

i try and get, 505 error =/
Quote Reply
Re: [NamedRisk] Dumb question In reply to
How about this;

Code:
# Loop through the top ten
for (my $i =0; $i <= 9; $i++) {
print "$sorted[$i] ($data->{$sorted[$i]}->{email})" .
" has $data->{$sorted[$i]}->{votes} votes\n";
}

Unsure

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Dumb question In reply to
umm i think that i found...

it's use local vars... so if i put

my $n = 0;

and then use inside the foreach n++;

work good... but thanks any way =D

you help to discover this using into you for the my =D