Sorting an Array of Arrays

Sorting an Array of Arrays

am 14.03.2007 01:44:32 von Hardly Armchair

Hello List,

I have a data structure containing a bunch of strings in different groups:

$groups = [
[
'SSPDQR',
'SSPSDR',
'STSSER',
],
[
'CSANLH',
'CVANRD',
],
[...],
...,
];
etc.

I want these sorted first alphabetically within the group, and then
according to the number of member in the group.

I have this code and it works fine:

my @s_groups = ();
for my $i (0 .. $#$groups) {
my @temp = sort { $a cmp $b } @{ $groups->[$i] };
push @s_groups, \@temp;
}

@s_groups = sort { @$b <=> @$a } @s_groups;

However, I have a feeling that this can be optimized due to its awkward
appearance. Does anyone have any ideas as to how to tighten up this
code? I'm trying to think of a way to incorporate a Schwartzian
Transform, but am unsure how to do so on this particular data structure.

This is not a critical question since the code works, but I'd appreciate
input all the same.

Thanks in advance for any help.

Sincerely,
Adam

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Sorting an Array of Arrays

am 14.03.2007 02:04:59 von krahnj

Hardly Armchair wrote:
> Hello List,

Hello,

> I have a data structure containing a bunch of strings in different groups:
>
> $groups = [
> [
> 'SSPDQR',
> 'SSPSDR',
> 'STSSER',
> ],
> [
> 'CSANLH',
> 'CVANRD',
> ],
> [...],
> ...,
> ];
> etc.
>
> I want these sorted first alphabetically within the group, and then
> according to the number of member in the group.

$ perl -le'
use Data::Dumper;
my $groups = [
[ qw/ STSSER SSPSDR SSPDQR / ],
[ qw/ CVANRD CSANLH / ],
];
print Dumper $groups;
my @s_groups = map $_->[ 1 ],
sort { $a->[ 0 ] <=> $b->[ 0 ] }
map [ scalar @$_, [ sort @$_ ] ],
@$groups;
print Dumper \@s_groups;
'
$VAR1 = [
[
'STSSER',
'SSPSDR',
'SSPDQR'
],
[
'CVANRD',
'CSANLH'
]
];

$VAR1 = [
[
'CSANLH',
'CVANRD'
],
[
'SSPDQR',
'SSPSDR',
'STSSER'
]
];



John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Sorting an Array of Arrays

am 14.03.2007 02:36:25 von mumia.w.18.spam+nospam

On 03/13/2007 07:44 PM, Hardly Armchair wrote:
> Hello List,
>
> I have a data structure containing a bunch of strings in different groups:
> [...]
>
> I want these sorted first alphabetically within the group, and then
> according to the number of member in the group.
> [...]

This is slightly more compact way to do it:

@s_groups = map { [ sort { $a cmp $b } @$_ ]; } @$groups;
@s_groups = sort { @$a <=> @$b } @s_groups;

I hope this helps.




--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Sorting an Array of Arrays

am 14.03.2007 15:31:38 von merlyn

>>>>> ""Mumia" == "Mumia W " writes:

"Mumia> On 03/13/2007 07:44 PM, Hardly Armchair wrote:
>> Hello List,
>> I have a data structure containing a bunch of strings in different groups:
>> [...]
>> I want these sorted first alphabetically within the group, and then
>> according to the number of member in the group.
>> [...]

"Mumia> This is slightly more compact way to do it:

"Mumia> @s_groups = map { [ sort { $a cmp $b } @$_ ]; } @$groups;

Uh,

sort { $a cmp $b } @input

is just a complex way to say:

sort @input

Since string sorting is the default.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/