sorting with Swartz Transform

I am trying to simplify some of my code. The output I am expecting is:
1 17.0.0.1
5 27.0.0.1
5 127.0.0.1
5 209.0.0.1
6 10.0.0.1
10 127.0.1.1

where the first colomn is the main sorting column, and if there are
multiples of that value, the ip should be sorted.

I have code the performs the task, but it's a Swartzian Transform, a
temporary array, another swartz transform, and just feels icky.

any suggestions to pretty this bloat ?


#!/usr/local/bin/perl

$hash{'127.0.0.1'} = 5;
$hash{'127.0.1.1'} = 10;
$hash{'27.0.0.1'} = 5;
$hash{'10.0.0.1'} = 6;
$hash{'17.0.0.1'} = 1;
$hash{'209.0.0.1'} = 5;

my [at] ipsorted = map { $_->[0] }
sort { $a->[1] <=> $b->[1] }
map { [$_,split /\./] } keys %hash;

foreach my $ip ( [at] ipsorted){
push [at] unsorted, "$hash{$ip} $ip";
}

foreach my $sorted (map { $_->[0] }
sort { $a->[1] <=> $b->[1] }
map { [$_,split] } [at] unsorted){
print "$sorted\n";
}


--

Jeremy Kister
http://jeremy.kister.net/


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Jeremy Kister [ So, 28 November 2004 09:02 ] [ ID #506563 ]

Re: sorting with Swartz Transform

Jeremy Kister wrote:
> I am trying to simplify some of my code. The output I am expecting is:
> 1 17.0.0.1
> 5 27.0.0.1
> 5 127.0.0.1
> 5 209.0.0.1
> 6 10.0.0.1
> 10 127.0.1.1
>
> where the first colomn is the main sorting column, and if there are
> multiples of that value, the ip should be sorted.
>
> I have code the performs the task, but it's a Swartzian Transform, a
> temporary array, another swartz transform, and just feels icky.
>
> any suggestions to pretty this bloat ?

my [at] unsorted = (
'5 127.0.0.1',
'10 127.0.1.1',
'5 27.0.0.1',
'6 10.0.0.1',
'1 17.0.0.1',
'5 209.0.0.1',
);

my [at] sorted = map { $_->[0] } sort {
$a->[1] <=> $b->[1]
||
$a->[2] <=> $b->[2]
||
$a->[3] <=> $b->[3]
||
$a->[4] <=> $b->[4]
||
$a->[5] <=> $b->[5]
} map { [ $_, split /[. ]/ ] } [at] unsorted;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Gunnar Hjalmarsson [ So, 28 November 2004 09:36 ] [ ID #506564 ]

Re: sorting with Swartz Transform

A very good lesson to learn Schwartzian Transform.
Thanks Gunnar!

--
Regards,
Edward WIJAYA
SINGAPORE

On Sun, 28 Nov 2004 09:36:26 +0100, Gunnar Hjalmarsson <noreply [at] gunnar.cc>
wrote:


>> I have code the performs the task, but it's a Swartzian Transform, a
>> temporary array, another swartz transform, and just feels icky.
>> any suggestions to pretty this bloat ?
>
> my [at] unsorted = (
> '5 127.0.0.1',
> '10 127.0.1.1',
> '5 27.0.0.1',
> '6 10.0.0.1',
> '1 17.0.0.1',
> '5 209.0.0.1',
> );
>
> my [at] sorted = map { $_->[0] } sort {
> $a->[1] <=> $b->[1]
> ||
> $a->[2] <=> $b->[2]
> ||
> $a->[3] <=> $b->[3]
> ||
> $a->[4] <=> $b->[4]
> ||
> $a->[5] <=> $b->[5]
> } map { [ $_, split /[. ]/ ] } [at] unsorted;
>




--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Edward wijaya [ So, 28 November 2004 09:47 ] [ ID #506565 ]

Re: sorting with Swartz Transform

On Sunday, November 28, 2004 at 3:36am, Gunnar Hjalmarsson wrote:
> >> any suggestions to pretty this bloat ?
> >
> >
> > my [at] unsorted = (
> > '5 127.0.0.1',
> > '10 127.0.1.1',
> > '5 27.0.0.1',
> > '6 10.0.0.1',
> > '1 17.0.0.1',
> > '5 209.0.0.1',
> > );
> >
> > my [at] sorted = map { $_->[0] } sort {
> > $a->[1] <=> $b->[1]
> > ||
> > $a->[2] <=> $b->[2]
> > ||
> > $a->[3] <=> $b->[3]
> > ||
> > $a->[4] <=> $b->[4]
> > ||
> > $a->[5] <=> $b->[5]
> > } map { [ $_, split /[. ]/ ] } [at] unsorted;


wild.. But since the data is already in a hash, are you suggesting to first
loop through the hash, pushing the data into an array, and then running the
sort? just because that's not much different from what I already have..

--

Jeremy Kister
http://jeremy.kister.net/


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Jeremy Kister [ Mo, 29 November 2004 03:45 ] [ ID #507610 ]

Re: sorting with Swartz Transform

Jeremy Kister wrote:
> I am trying to simplify some of my code. The output I am expecting is:
> 1 17.0.0.1
> 5 27.0.0.1
> 5 127.0.0.1
> 5 209.0.0.1
> 6 10.0.0.1
> 10 127.0.1.1
>
> where the first colomn is the main sorting column, and if there are
> multiples of that value, the ip should be sorted.
>
> I have code the performs the task, but it's a Swartzian Transform, a
> temporary array, another swartz transform, and just feels icky.
>
> any suggestions to pretty this bloat ?
>
>
> #!/usr/local/bin/perl
>
> $hash{'127.0.0.1'} = 5;
> $hash{'127.0.1.1'} = 10;
> $hash{'27.0.0.1'} = 5;
> $hash{'10.0.0.1'} = 6;
> $hash{'17.0.0.1'} = 1;
> $hash{'209.0.0.1'} = 5;
>
> my [at] ipsorted = map { $_->[0] }
> sort { $a->[1] <=> $b->[1] }
> map { [$_,split /\./] } keys %hash;
>
> foreach my $ip ( [at] ipsorted){
> push [at] unsorted, "$hash{$ip} $ip";
> }
>
> foreach my $sorted (map { $_->[0] }
> sort { $a->[1] <=> $b->[1] }
> map { [$_,split] } [at] unsorted){
> print "$sorted\n";
> }

Beware the costs of C<map>, C<grep>, and the like. They are sometimes
very handy, but remember that each one represents an iteration through
the entire list. In your above example, there are at least 8 (possibly
9?) iterations through the list: 4 map operations, 2 sorts, 2 explicit
loops, and 1 keys op.

In addition to the cost in speed of the multiple iterations, there is
also an unseen temporary list created to hold the results of each of
those C<map>, C<sort>, and <keys> operations. I'm not sure how long the
temporaries are held, but there has to be atleast 3 copies present in
memory for each of operations in the Swartz Transform: 1) the original,
2) the list being iterated over, ie the right-hand side argument, and 3)
the result list being created.

That's a pretty big cost in speed and size. For your example, you're
probably better off writing one explicit loop which takes the data in
%hash and places it into a more appropriate data structure. After it's
in a more appropriate structure you can sort it. (That's 2, maybe three
iterations, and 1 temporary... I think).

Randy.

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
ml-perl [ Mo, 29 November 2004 06:03 ] [ ID #507613 ]

Re: sorting with Swartz Transform

Jeremy Kister wrote:
> I am trying to simplify some of my code. The output I am expecting is:
> 1 17.0.0.1
> 5 27.0.0.1
> 5 127.0.0.1
> 5 209.0.0.1
> 6 10.0.0.1
> 10 127.0.1.1
>
> where the first colomn is the main sorting column, and if there are
> multiples of that value, the ip should be sorted.
>
> I have code the performs the task, but it's a Swartzian Transform, a
> temporary array, another swartz transform, and just feels icky.
>
> any suggestions to pretty this bloat ?
>
> #!/usr/local/bin/perl
>
> $hash{'127.0.0.1'} = 5;
> $hash{'127.0.1.1'} = 10;
> $hash{'27.0.0.1'} = 5;
> $hash{'10.0.0.1'} = 6;
> $hash{'17.0.0.1'} = 1;
> $hash{'209.0.0.1'} = 5;
>
> my [at] ipsorted = map { $_->[0] }
> sort { $a->[1] <=> $b->[1] }
> map { [$_,split /\./] } keys %hash;
>
> foreach my $ip ( [at] ipsorted){
> push [at] unsorted, "$hash{$ip} $ip";
> }
>
> foreach my $sorted (map { $_->[0] }
> sort { $a->[1] <=> $b->[1] }
> map { [$_,split] } [at] unsorted){
> print "$sorted\n";
> }


The Schwartzian Transform is good but it doesn't beat the Guttman Rosler
Transform. :-)

#!/usr/local/bin/perl
use warnings;
use strict;

use Socket;

my %hash = (
'127.0.0.1' => 5,
'127.0.1.1' => 10,
'27.0.0.1' => 5,
'10.0.0.1' => 6,
'17.0.0.1' => 1,
'209.0.0.1' => 5,
);

print map /\0([^\0]+)\z/,
sort
map pack( 'n', $hash{ $_ } ) . inet_aton( $_ ) . "\0$hash{$_} $_\n",
keys %hash;

__END__


John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
krahnj [ Mo, 29 November 2004 08:29 ] [ ID #507912 ]

Re: sorting with Swartz Transform

Jeremy Kister wrote:
> Gunnar Hjalmarsson wrote:
>>
>> my [at] unsorted = (
>> '5 127.0.0.1',
>> '10 127.0.1.1',
>> '5 27.0.0.1',
>> '6 10.0.0.1',
>> '1 17.0.0.1',
>> '5 209.0.0.1',
>> );
>>
>> my [at] sorted = map { $_->[0] } sort {
>> $a->[1] <=> $b->[1]
>> ||
>> $a->[2] <=> $b->[2]
>> ||
>> $a->[3] <=> $b->[3]
>> ||
>> $a->[4] <=> $b->[4]
>> ||
>> $a->[5] <=> $b->[5]
>> } map { [ $_, split /[. ]/ ] } [at] unsorted;
>
> wild.. But since the data is already in a hash,

Didn't know that was a prerequisite.

> are you suggesting to first loop through the hash, pushing the data
> into an array, and then running the sort?

No, the point was to show that it can be done with one sort. Given the
hash you can do:

print "$hash{$_} $_\n" for map { $_->[0] } sort {
$hash{ $a->[0] } <=> $hash{ $b->[0] }
||
$a->[1] <=> $b->[1]
||
$a->[2] <=> $b->[2]
||
$a->[3] <=> $b->[3]
||
$a->[4] <=> $b->[4]
} map { [ $_, split /\./ ] } keys %hash;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Gunnar Hjalmarsson [ Mo, 29 November 2004 13:45 ] [ ID #508529 ]

Re: sorting with Swartz Transform

>>>>> "John" == John W Krahn <krahnj [at] telus.net> writes:

John> The Schwartzian Transform is good but it doesn't beat the Guttman
John> Rosler Transform. :-)

For specific cases. For the general case, the ST is still the most
flexible.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn [at] stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
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 [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
merlyn [ Di, 30 November 2004 16:55 ] [ ID #510808 ]

Re: sorting with Swartz Transform

Randal L. Schwartz wrote:
>>>>>>"John" == John W Krahn <krahnj [at] telus.net> writes:
>
> John> The Schwartzian Transform is good but it doesn't beat the Guttman
> John> Rosler Transform. :-)
>
> For specific cases. For the general case, the ST is still the most
> flexible.

But of course I knew that! :-)
I should have said "but it doesn't beat the Guttman Rosler Transform for speed."


John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
krahnj [ Di, 30 November 2004 17:20 ] [ ID #510810 ]

Re: sorting with Swartz Transform

>>>>> "John" == John W Krahn <krahnj [at] telus.net> writes:

John> But of course I knew that! :-)
John> I should have said "but it doesn't beat the Guttman Rosler
John> Transform for speed."

Yes. The ST can sort any multilevel complex sort, and is "programmer
efficient". When the GRT *can* be used, and the programmer is willing
to invest time to compute the "transform to and from a single string"
functions, the GRT *may* be faster.

It's like assembly vs high-level programming. Sure, assembly will be
faster, but in what way? Certainly not in maintenance or readability.

print "Just another Perl hacker,";

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn [at] stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
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 [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
merlyn [ Mi, 01 Dezember 2004 17:03 ] [ ID #513304 ]
Perl » gmane.comp.lang.perl.beginners » sorting with Swartz Transform

Vorheriges Thema: Reg. Associative array in perl
Nächstes Thema: Running Perl Scripts Via A Web Page