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 Data

 



xcom
stranger

May 20, 2001, 12:26 PM

Post #1 of 3 (1153 views)
Sorting Data Can't Post

Ok. I am so stumped. Anyone...Please help!
I have a txt file with data stored like this:

ID=a201&url=http://www.excite.com
ID=a201&url=http://www.excite.com
ID=a202&url=http://www.yahoo.com
ID=a201&url=http://www.yahoo.com
ID=a202&url=http://www.altavista.com

How in the worl can I get a script to open this file and print out the data like this:

referring url's for partner a201

referer-----------------------------------------Hits
http://www.excite.com------------------2
http://www.yahoo.com------------------1

Total Hits-------------------------------------3

And alternately like this:
Partner -------------------------Hits
Partner a201------------------3
Partner a202------------------2

Total Hits-----------------------5

This is probably a piece of cake to a lot of you,but I am new to this and have tried and tried,and cannot seem to get it to work. Any help is MUCH apreciated!




mhx
Enthusiast / Moderator

May 21, 2001, 4:35 AM

Post #2 of 3 (1138 views)
Re: Sorting Data [In reply to] Can't Post

Try this:

Code
#!/bin/perl -w 
use strict;

my $file = "myfile.txt";
my(%part, %hits);

# Read file
open FILE, $file or die "cannot open '$file': $!\n";
while( <FILE> ) {
$part{$1}{$2}++ if /ID=([^&]+)&url=([^&\n]+)/;
}
close FILE;

# Standard Printout
foreach my $partner ( sort keys %part ) {
my $total = 0;
print "\nreferring url's for partner $partner:\n\n";
foreach( sort keys %{$part{$partner}} ) {
printf "%-50s =\n", $_, $part{$partner}{$_};
$total += $part{$partner}{$_};
}
printf "\n%-50s =\n", "Total Hits", $total;
$hits{$partner} = $total;
}

# Alternative Printout
my $total = 0;
print "\n\nTotal hits by partner:\n\n";
foreach( sort keys %hits ) {
printf "%-50s =\n", $_, $hits{$_};
$total += $hits{$_};
}
printf "\n%-50s =\n\n", "Total Hits", $total;

Output is not very nice yet, but it works...

-- Marcus



xcom
stranger

May 22, 2001, 12:07 AM

Post #3 of 3 (1124 views)
Re: Sorting Data [In reply to] Can't Post

Hey Thanks Marcus.
I actually got this one covered now and is working great! God I love Perl! But god how I wish it wasnt so damn picky! LOL!

Anyways,thanks for your help!


 
 


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

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