RE: Sorting problem

2002-05-06 Thread Nikola Janceski

take out the spaces

@sorted = {$a-[2] = $b-[2]} @AoA but this gives a cannot
   ^   ^
No spaces should be here


 -Original Message-
 From: Richard Adams [mailto:[EMAIL PROTECTED]]
 Sent: Monday, May 06, 2002 10:18 AM
 To: [EMAIL PROTECTED]
 Subject: Sorting problem
 
 
 Hello,
  I've got an array of arrays, and want to sort by the 3rd 
 element of the
 subarray. I then want to print out the sorted array, showing 
 the index and
 values. E.g.,
 
 @AoA = (
 [23.56, 65.2, 12.4],
 [13, 56, 87],
 [45,876, 23],
 etc
 )
 
 
 And then the printout should look like:
 1:  87
 2:  23
 3:  12.4
 
 I've tried 
 @sorted = {$a -[2] = $b -[2]} @AoA but this gives a cannot
 modify..error.
 I've a feeling this is really trivial but have got abit 
 bogged down with
 it...
 Thanks in advance
 Richard
 
 -- 
 Dr Richard Adams
 Chromosome Structure Group
 
 University of Edinburgh
 Kings Buildings,
 Mayfield Rd,
 Edinburgh
 EH9 3JR UK
 
 
 Email [EMAIL PROTECTED]
 
 
 -- 
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 



The views and opinions expressed in this email message are the sender's
own, and do not necessarily represent the views and opinions of Summit
Systems Inc.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Sorting problem

2002-05-06 Thread Bob Showalter

 -Original Message-
 From: Richard Adams [mailto:[EMAIL PROTECTED]]
 Sent: Monday, May 06, 2002 10:18 AM
 To: [EMAIL PROTECTED]
 Subject: Sorting problem
 
 
 Hello,
  I've got an array of arrays, and want to sort by the 3rd 
 element of the
 subarray. I then want to print out the sorted array, showing 
 the index and
 values. E.g.,
 
 @AoA = (
 [23.56, 65.2, 12.4],
 [13, 56, 87],
 [45,876, 23],
 etc
 )
 
 
 And then the printout should look like:
 1:  87
 2:  23
 3:  12.4
 
 I've tried 
 @sorted = {$a -[2] = $b -[2]} @AoA but this gives a cannot
 modify..error.
 I've a feeling this is really trivial but have got abit 
 bogged down with
 it...

You forgot the word sort!

   @sorted = sort {$a-[2] = $b-[2]} @AoA;
 

Also, your example output indicates you probably want to
sort in descending order, which would be:

   @sorted = sort {$b-[2] = $a-[2]} @AoA;

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Sorting problem

2002-05-06 Thread Felix Geerinckx

on Mon, 06 May 2002 14:17:39 GMT, [EMAIL PROTECTED] (Richard
Adams) wrote: 

[Please don't retype code - cut and paste instead]

 I've tried 
 @sorted = {$a -[2] = $b -[2]} @AoA 

You forgot the 'sort' function.
You put spaces before the '-'.
You got the sort order reversed.

Try

my @sorted = sort {$b-[2] = $a-[2]} @AoA;

for my $i(0..$#sorted) {
print $i+1, :\t$sorted[$i][2]\n;
}

-- 
felix

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Sorting problem

2002-05-06 Thread Michael Lamertz

On Mon, May 06, 2002 at 03:17:39PM +0100, Richard Adams wrote:
 Hello,
  I've got an array of arrays, and want to sort by the 3rd element of the
 subarray. I then want to print out the sorted array, showing the index and
 values. E.g.,

Hmm, works for me with the exception of...

 I've tried 
 @sorted = {$a -[2] = $b -[2]} @AoA but this gives a cannot

there's a 'sort' missing before the block:

-- mostly your code --
#!/usr/bin/perl

use strict;
use warnings;

my @AoA = (
[23.56, 65.2, 12.4],
[13, 56, 87],
[45,876, 23],
);

my @sorted = sort {$a-[2] = $b-[2]} @AoA;
print $_-[2]\n foreach reverse @sorted;
-- mostly your code --

gives me:

-- snip --
nijushiho:/tmp$ perl x.pl
87
23
12.4
nijushiho:/tmp$ 
-- snip --

-- 
   If we fail, we will lose the war.

Michael Lamertz|  +49 221 445420 / +49 171 6900 310
Nordstr. 49|   [EMAIL PROTECTED]
50733 Cologne  | http://www.lamertz.net
Germany|   http://www.perl-ronin.de 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Sorting problem

2002-05-06 Thread Richard Adams

Thanks for all your help...I just forgot the sort in my post...

The reason it wasn't working wasn't the sort, it was just that $AoA[0] was
undefined..
But sort {$a-[2] = $b-[2]} @AoA[1..$#AoA] works just fine.
Thanks again!


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Sorting problem-part2

2002-05-06 Thread Richard Adams

Now I've got an array of hashes, where each hash can have  different keys
e.g.,

@aoh = ( {H3 =234, H1 =127, DNA =1, p135 =167},
{H4=24,  H3=1550, DNA =25, p39 =67},
{H3 =34, H2A =125, DNA =5, p32 =7},
{H3 =24, H4 =156, DNA =123, p12 =13}
) 
And I'd like to order the array elements by virtue of the biggest value in
the hash. 

1. H3 1550
2. H3 234
3. H4 156
4. H2A 125

Is there a quick one liner for this or do I need to find the max in each
hash, store them in an array, and then sort the array of hash maxima
separately, while maintaining the key/value associations?

Thanks a lot for any suggestions!
Richard


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Sorting problem-part2

2002-05-06 Thread David Gray

 Now I've got an array of hashes, where each hash can have  
 different keys e.g.,
 
 @aoh = ( {H3 =234, H1 =127, DNA =1, p135 =167},
 {H4=24,  H3=1550, DNA =25, p39 =67},
 {H3 =34, H2A =125, DNA =5, p32 =7},
 {H3 =24, H4 =156, DNA =123, p12 =13}
 ) 
 And I'd like to order the array elements by virtue of the 
 biggest value in the hash. 
 
 1. H3 1550
 2. H3 234
 3. H4 156
 4. H2A 125
 
 Is there a quick one liner for this or do I need to find 
 the max in each hash, store them in an array, and then sort 
 the array of hash maxima separately, while maintaining the 
 key/value associations?

perldoc -q sort a hash

 -dave



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Sorting problem-part2

2002-05-06 Thread Michael Lamertz

On Mon, May 06, 2002 at 12:22:15PM -0400, David Gray wrote:
  Now I've got an array of hashes, where each hash can have  
  different keys e.g.,
  
  @aoh = ( {H3 =234, H1 =127, DNA =1, p135 =167},
  {H4=24,  H3=1550, DNA =25, p39 =67},
  {H3 =34, H2A =125, DNA =5, p32 =7},
  {H3 =24, H4 =156, DNA =123, p12 =13}
  ) 
  And I'd like to order the array elements by virtue of the 
  biggest value in the hash. 



 perldoc -q sort a hash

Nope, he's looking for a *nested* sort, since he has to find the largest
value 'values %$a' and 'values %$b'.

And as a nice addon, he mentions that the keys of the hashes can vary.

-- 
   If we fail, we will lose the war.

Michael Lamertz|  +49 221 445420 / +49 171 6900 310
Nordstr. 49|   [EMAIL PROTECTED]
50733 Cologne  | http://www.lamertz.net
Germany|   http://www.perl-ronin.de 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Sorting problem-part2

2002-05-06 Thread Michael Lamertz

Took me some time, but...

On Mon, May 06, 2002 at 04:41:03PM +0100, Richard Adams wrote:
 Now I've got an array of hashes, where each hash can have  different keys
 e.g.,
 
 @aoh = ( {H3 =234, H1 =127, DNA =1, p135 =167},
 {H4=24,  H3=1550, DNA =25, p39 =67},
 {H3 =34, H2A =125, DNA =5, p32 =7},
 {H3 =24, H4 =156, DNA =123, p12 =13}
 ) 
 And I'd like to order the array elements by virtue of the biggest value in
 the hash. 
 
 1. H3 1550
 2. H3 234
 3. H4 156
 4. H2A 125

I'll call it the Partial Schwartzian Transformation on Acid ;-)

Ok, ok, two messages ago I called Jason Friswold evil, but I just
couldn't resist.  :-


So, this one's not for the faint of heart (tm), and for educational
purposes only:

-- snip --
#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

my @aoh = (
{H3 =234, H1 =127, DNA =1, p135 =167},
{H4=24,  H3=1550, DNA =25, p39 =67},
{H3 =34, H2A =125, DNA =5, p32 =7},
{H3 =24, H4 =156, DNA =123, p12 =13}
);

my @xsorted = sort {
$b-[0]{$b-[1]} = $a-[0]{$a-[1]};
} map {
[ $_, (sort { $_-{$b} = $_-{$a} } keys %$_)[0] ]
} @aoh;
print Dumper(\@xsorted);

print $_-[1]: $_-[0]{$_-[1]}\n foreach @xsorted;
-- snip --

Let's dissect that a littlebit:

First of all, the Schwartzian Transformation is usually used as an
optimization for sorting.  You use it if the 'per-compare' computation
is rather difficult.  For an array @l it goes like this:

A. create a temp list from @l with
a. the actual data from @l
b. the precomputed data from @l that's used for comparing 2
   elements during sorting.

B. sort that temp list using the precomputed field.

C. create the sorted list by extracting the 'pure' data from @l from
  the now sorted temp list

D. do it all in a fat combination of map sort map :-

We need to skip step C, since we'd lose the information about the
highest hash value if we just put all the data into the resulting array
again.

If you reverse-read above '@xsorted = ...' code you find

A. create a temp list from @l..:

map {
[
  # ...with the actual data from @l...
  $_,
  # ...and the precomputed...
  (sort { $_-{$b} = $_-{$a} } keys %$_)[0]
]
} @aoh

   For each element in @aoh we're sorting the keys of that hash by
   comparing the hash's value at that key.

   Then we use only the first element of that resulting sorted list
   of hash keys, since that has the highest value.

   So, now we have a list
(
  [ original_item, highest_hash_key_for_that_item ],
  ...
)


B. sort that temp list using the precomputed fields...

sort {
$b-[0]{$b-[1]} = $a-[0]{$a-[1]}
}

   We can split that up and be a bit more verbose:

sort {
my ($a_orig_item, $a_key) = @$a;
my ($b_orig_item, $b_key) = @$b;

$b_orig_item-{$b_key} = $a_orig_item-{$a_key}
}

   which is a bit longer but doesn't scare the kids that much.

Now on for the questions and flames...

-- 
   If we fail, we will lose the war.

Michael Lamertz|  +49 221 445420 / +49 171 6900 310
Nordstr. 49|   [EMAIL PROTECTED]
50733 Cologne  | http://www.lamertz.net
Germany|   http://www.perl-ronin.de 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]