CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Sorting A Data File

 



NeedHelp
Deleted

May 14, 2000, 10:39 PM

Post #1 of 8 (1565 views)
Sorting A Data File Can't Post

Hi,

I am having trouble sorting from a data file.

My code is as follows:

open(FILE, "$tbase") | | die "I can't open that because: $!\n";
while(<FILE> ) {
chop;
@all = split(/\n/);
foreach $line (@all) {
local($team, $w, $l, $t, $pts, $pf, $pa, $pct) = split(/&&/, $line);

The variable $pct is a number value that starts with a decimal(.346,.485,.555). I want to sort that variable in order from greatest to least and print it back out to the screen with all the other variables.

Can somebody help me?


mckhendry
Deleted

May 15, 2000, 11:45 AM

Post #2 of 8 (1565 views)
Re: Sorting A Data File [In reply to] Can't Post

Hi,

If I understand you correctly what you may want to do is have either an array of hashes or an array of arrays. This way you can keep the data from each line together and still sort according to one category. The 'Advanced Perl Programming' book by O'Reilly talks about how to do this in the first chapter. If you need to know more about how to do this let me know. Good Luck!


NeedHelp
Deleted

May 15, 2000, 12:02 PM

Post #3 of 8 (1565 views)
Re: Sorting A Data File [In reply to] Can't Post

Can you show me an example?


tallest1
Deleted

May 15, 2000, 12:25 PM

Post #4 of 8 (1565 views)
Re: Sorting A Data File [In reply to] Can't Post

A while back, I had the same problem you did. Fortunately, I had found this useful page( http://www.pagesz.net/~scotty/perlscripts/flatfiles.html ) with some good info on sorting flat file db's. I hope this helps


mckhendry
Deleted

May 15, 2000, 1:20 PM

Post #5 of 8 (1565 views)
Re: Sorting A Data File [In reply to] Can't Post

In this example I grab the info from a database and store it in an array of hashes. Each element of the array is a reference to an anonymous hash. I would use the syntax $issues[$i]->{'resourceID'} to get the value out of it. This might be overdoing it for what you are trying to do, but then again, maybe not.

my $query = "select description,
resourceID,
taskDate,
timeSpent from taskhistory where taskID = $taskID";
my $rst = $dbh->prepare($query);
$rst->execute();
my @history;
while (my @temp = $rst->fetchrow_array)
{
my $rh = {'description'=>$temp[0],'resourceID'=>$temp[1],'taskDate'=>$temp[2],'timeSpent'=>$temp[3]};

push (@history, $rh);
}
$rst->finish();


Cure
User

May 15, 2000, 2:31 PM

Post #6 of 8 (1565 views)
Re: Sorting A Data File [In reply to] Can't Post

He wusnt asking how to fetch information from a sql database.

NeedHelp I would recommend a Perl Book called Perl Programming..


Cure


NeedHelp
Deleted

May 16, 2000, 7:54 AM

Post #7 of 8 (1565 views)
Re: Sorting A Data File [In reply to] Can't Post

Thank You guys for the book recomendations. Unfortunately I don't have those books and I still need help with my problem. If anybody has an example or an explanation please help me!


mckhendry
Deleted

May 16, 2000, 11:18 AM

Post #8 of 8 (1565 views)
Re: Sorting A Data File [In reply to] Can't Post

NeedHelp,
this script is an example of how to do what you are trying. But there is one condition, it is that no $pct are ever the same. If so then you'll have duplicates in your hash and will have to basically write your own sorting algorithm. I hope this helps. Like Cure says your best bet would be to get an O'Reilly book. Good Luck!

#!/usr/bin/perl

use strict;

my %data;
my $tbase = "../test.txt";
print<<HTML;
Content-type: text/html

<html>
<head>
<title>
</title>
</head>
<body>
<center>
HTML
open(FILE, "$tbase") &#0124; &#0124; die "I can't open that because: $!\n";
while(<FILE> )
{
chop;
my @all = split(/\n/);
foreach my $line (@all)
{
my ($team, $w, $l, $t, $pts, $pf, $pa, $pct) = split(/&&/, $line);
my @info = ($team, $w, $l, $t, $pts, $pf, $pa);
my $ra = \@info;
$data{$pct} = $ra;
}
}
foreach my $line (sort numerically keys %data)
{
print "$line $data{$line}->[0] $data{$line}->[1] $data{$line}->[2] $data{$line}->[3] $data{$line}->[4] $data{$line}->[5] $data{$line}->[6]<br>";
}
print<<HTML;
</center>
</body>
</html>
HTML

sub numerically
{
$b <=> $a;
}

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives