sorting and calculating from tabulated data 
Author Message
 sorting and calculating from tabulated data

Here's a model of a tabulated data set I need to deal with:
a data Susan
a data John
b data Mary
a data Andy
a data Susan
c data Barry
c data Kate
b data Barry
...

I need to compute totals for each person by category sorted in the following
format:

Category a
    Susan 3
    Andy 1
    John 1
Catgory b
...
So far, I've come up with a rather crude semi-solution:

open(F,"hash.txt") || die "cannot open hash.txt: $!\n";
while(<F>) {

$hit = $cols[0] . "\t" . $cols[2];
$seen{$hit}++;

Quote:
}

close(F);

foreach $i (sort(keys %seen)) {
  print "$i $seen{$i}\n";

Quote:
}

But it doesn't really do what I want.  (I'm using perl 5_6 on Win XP)

TIA,

Rafal
--
Semper Fiddlis



Tue, 21 Sep 2004 08:31:21 GMT  
 sorting and calculating from tabulated data

Quote:

> Here's a model of a tabulated data set I need to deal with:
> a data Susan
> a data John
> b data Mary
> a data Andy
> a data Susan
> c data Barry
> c data Kate
> b data Barry
> ...

> I need to compute totals for each person by category sorted in the following
> format:

> Category a
>     Susan 3
>     Andy 1
>     John 1
> Catgory b
> ...
> So far, I've come up with a rather crude semi-solution:

Read the results of

  perldoc perldsc

and see if you can understand this:

#!/usr/bin/env perl

use warnings;
use strict;

my %seen;

while (<DATA>) {
    my ( $category, $name ) = (split)[ 0, 2 ];
    $seen{$category}{$name}++;

Quote:
}

foreach my $category ( sort keys %seen ) {
    print "Category $category\n";
    foreach my $name (
        sort { $seen{$category}{$b} <=> $seen{$category}{$a} || $a cmp $b }
        keys %{$seen{$category}} )
    {
        print "  $name $seen{$category}{$name}\n";
    }
Quote:
}

__END__
a data Susan
a data John
b data Mary
a data Andy
a data Susan
c data Barry
c data Kate
b data Barry

Hope this helps,

Mike

--

http://www.stok.co.uk/~mike/       | GPG PGP Key      1024D/059913DA

http://www.starnix.com/            |                  75D2 9EC4 C1C0 0599 13DA



Tue, 21 Sep 2004 13:07:57 GMT  
 sorting and calculating from tabulated data
Mike,

Very nice solution!  Many thanks.

I did understand most of the code, but I have a few questions:

Quote:
>     my ( $category, $name ) = (split)[ 0, 2 ];

Does "split" by default recognize \s as the separator? (the data file used
\t)
If I change the above to
    My($category,$name) = (split('#',$_))[0,2];
I *have* to chomp($_) to get the correct formatting; why does your code work
without the chomp?

Quote:
>         sort { $seen{$category}{$b} <=> $seen{$category}{$a} || $a cmp
$b }
>         keys %{$seen{$category}} )

I had suspected it would have to be a multi-dimensional array, but wasn't
sure how to do it.  This is beautiful.

Thanks once again.

Rafal

Quote:
----- Original Message -----

Newsgroups: comp.lang.perl
Sent: Friday, April 05, 2002 1:07 AM
Subject: Re: sorting and calculating from tabulated data

> and see if you can understand this:

> #!/usr/bin/env perl

> use warnings;
> use strict;

> my %seen;

> while (<DATA>) {
>     my ( $category, $name ) = (split)[ 0, 2 ];
>     $seen{$category}{$name}++;
> }

> foreach my $category ( sort keys %seen ) {
>     print "Category $category\n";
>     foreach my $name (
>         sort { $seen{$category}{$b} <=> $seen{$category}{$a} || $a cmp
$b }
>         keys %{$seen{$category}} )
>     {
>         print "  $name $seen{$category}{$name}\n";
>     }
> }
> __END__
> a data Susan
> a data John
> b data Mary
> a data Andy
> a data Susan
> c data Barry
> c data Kate
> b data Barry

> Hope this helps,

> Mike

> --

apply.
> http://www.stok.co.uk/~mike/       | GPG PGP Key      1024D/059913DA

3D60
> http://www.starnix.com/            |                  75D2 9EC4 C1C0 0599
13DA





> > Here's a model of a tabulated data set I need to deal with:
> > a data Susan
> > a data John
> > b data Mary
> > a data Andy
> > a data Susan
> > c data Barry
> > c data Kate
> > b data Barry
> > ...

> > I need to compute totals for each person by category sorted in the
following
> > format:

> > Category a
> >     Susan 3
> >     Andy 1
> >     John 1
> > Catgory b
> > ...
> > So far, I've come up with a rather crude semi-solution:

> Read the results of

>   perldoc perldsc

> and see if you can understand this:

> #!/usr/bin/env perl

> use warnings;
> use strict;

> my %seen;

> while (<DATA>) {
>     my ( $category, $name ) = (split)[ 0, 2 ];
>     $seen{$category}{$name}++;
> }

> foreach my $category ( sort keys %seen ) {
>     print "Category $category\n";
>     foreach my $name (
>         sort { $seen{$category}{$b} <=> $seen{$category}{$a} || $a cmp
$b }
>         keys %{$seen{$category}} )
>     {
>         print "  $name $seen{$category}{$name}\n";
>     }
> }
> __END__
> a data Susan
> a data John
> b data Mary
> a data Andy
> a data Susan
> c data Barry
> c data Kate
> b data Barry

> Hope this helps,

> Mike

> --

apply.
> http://www.stok.co.uk/~mike/       | GPG PGP Key      1024D/059913DA

3D60
> http://www.starnix.com/            |                  75D2 9EC4 C1C0 0599
13DA



Fri, 24 Sep 2004 00:13:43 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Summary : Problems reading tabulated data

2. Problem reading tabulated data...

3. Splitting space tabulated data

4. Sorting multiple-line data to single line data

5. newbie: flat file tabulating question

6. Perl script that tabulates polls

7. Sorting w/perl5 complex data structures

8. HELP: Need to sort an associative array by its DATA

9. Need Help using sort routine on flat text data - please

10. Sorting Algorithm: organize data into a tree

11. looking for a better way of converting data to sort in db

12. Sorting data file on numerical field

 

 
Powered by phpBB® Forum Software