Home > Archive > PERL Miscellaneous > May 2004 > sort numeric lists
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
sort numeric lists
|
|
| The King of Pots and Pans 2004-04-20, 9:35 pm |
| On disk I have a 2d table of numbers. Something like this:
9 8 6
4 5 6
I read it in so that I have a list of references to lists. In other
words, a list of the rows.
I want to first sort by column 2, then column 3. When I sort by column
2 I get:
4 5 6
9 8 6
As expected because 5 < 8. When I sort by column 3 I get:
9 8 6
4 5 6
This is unexpected. Since 6 = 6, I don't want it to perform the
sort. See how it was already sorted by column 2 when column 3 elements
were equal, but now column 2 is unsorted again.
--
The King of Pots and Pans
| |
| Gregory Toomey 2004-04-20, 10:30 pm |
| The King of Pots and Pans wrote:
> On disk I have a 2d table of numbers. Something like this:
>
> 9 8 6
> 4 5 6
>
> I read it in so that I have a list of references to lists. In other
> words, a list of the rows.
>
> I want to first sort by column 2, then column 3. When I sort by column
> 2 I get:
>
> 4 5 6
> 9 8 6
>
> As expected because 5 < 8. When I sort by column 3 I get:
>
> 9 8 6
> 4 5 6
>
> This is unexpected. Since 6 = 6, I don't want it to perform the
> sort.
Then dont.
> See how it was already sorted by column 2 when column 3 elements
> were equal, but now column 2 is unsorted again.
What are you trying to do? Partial pivoting for Gaussian elimination? Tensor
algebra?
The Perl sort function works on lists.
gtoomey
| |
| Brad Baxter 2004-04-20, 10:30 pm |
| On Wed, 21 Apr 2004, The King of Pots and Pans wrote:
> On disk I have a 2d table of numbers. Something like this:
>
> 9 8 6
> 4 5 6
>
> I read it in so that I have a list of references to lists. In other
> words, a list of the rows.
>
> I want to first sort by column 2, then column 3. When I sort by column
> 2 I get:
>
> 4 5 6
> 9 8 6
>
> As expected because 5 < 8. When I sort by column 3 I get:
>
> 9 8 6
> 4 5 6
>
> This is unexpected. Since 6 = 6, I don't want it to perform the
> sort. See how it was already sorted by column 2 when column 3 elements
> were equal, but now column 2 is unsorted again.
Typically, a sort algorithm will NOT preserve the original order when
sorting equal values. It's extra work that slows things down. It may be
unexpected, but is common. Often, sort routines offer an option that WILL
preserve original order. Perl's isn't one (that I'm aware--though I may
be mistaken).
I'm being terse in that explanation. Others may elaborate.
Regards,
Brad
| |
| Uri Guttman 2004-04-20, 10:30 pm |
| >>>>> "BB" == Brad Baxter <bmb@ginger.libs.uga.edu> writes:
BB> On Wed, 21 Apr 2004, The King of Pots and Pans wrote:[color=darkred]
BB> Typically, a sort algorithm will NOT preserve the original order
BB> when sorting equal values. It's extra work that slows things
BB> down. It may be unexpected, but is common. Often, sort routines
BB> offer an option that WILL preserve original order. Perl's isn't
BB> one (that I'm aware--though I may be mistaken).
wrong on several counts. perl's current default sort algorithm is stable
(which means it keeps records with the same key in their original order
relative to each other). and it is not extra work to have a stable sort,
it is just a different algorithm. both the old sort (which you can still
get via a pragma) and the new one are O(N log N) on average which means
they are the same over all speed.
anyhow, the OP's problem has nothing to do with stable sorting. his
problem is that he needs to do a multikey sort and didn't know how to
specify that or do it. and of course with no code shown, who could guess
what he really did. i am using PSI::ESP and my knowledge of sorting to
figure it out. his comment on sorting by column 1 and THEN 2 and THEN 3
makes it seem like he called sort 3 separate times which is so wrong.
so if the OP wants to learn about multikey sorting in perl he should
read this:
http://www.sysarch.com/perl/sort_paper.html
and the promised module in that paper (5 years ago) is actually under
full development right now and mostly working. it is the core of a talk
i am giving at yapc::buffalo this june. the module is called Sort::Maker
and this is what the OP would do to properly sort his rows of numbers
use Sort::Maker ;
my $sorter = make_sorter(
plain => 1,
number => '$_->[0]',
number => '$_->[1]',
number => '$_->[2]',
) ;
my @sorted = $sorter->( @unsorted ) ;
i may have an alpha version of this module ready in a few w s if
anyone is interested in playing with it. the goal is to have it on cpan
before yapc which is in mid-june.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
| |
| Jim Cochrane 2004-04-20, 11:32 pm |
| In article <fdjhc.65339$U83.9813@fed1read03>, The King of Pots and Pans wrote:
> On disk I have a 2d table of numbers. Something like this:
>
> 9 8 6
> 4 5 6
>
> I read it in so that I have a list of references to lists. In other
> words, a list of the rows.
>
> I want to first sort by column 2, then column 3. When I sort by column
> 2 I get:
>
> 4 5 6
> 9 8 6
>
> As expected because 5 < 8. When I sort by column 3 I get:
>
> 9 8 6
> 4 5 6
>
> This is unexpected. Since 6 = 6, I don't want it to perform the
> sort. See how it was already sorted by column 2 when column 3 elements
> were equal, but now column 2 is unsorted again.
>
Why don't you post your code and let people help you fix it? Otherwise,
you may not get much useful help.
--
Jim Cochrane; jtc@dimensional.com
[When responding by email, include the term non-spam in the subject line to
get through my spam filter.]
| |
| Brad Baxter 2004-04-21, 12:31 am |
| On Wed, 21 Apr 2004, Uri Guttman wrote:
>
> BB> Typically, a sort algorithm will NOT preserve the original order
> BB> when sorting equal values. It's extra work that slows things
> BB> down. It may be unexpected, but is common. Often, sort routines
> BB> offer an option that WILL preserve original order. Perl's isn't
> BB> one (that I'm aware--though I may be mistaken).
>
> wrong on several counts. perl's current default sort algorithm is stable
> (which means it keeps records with the same key in their original order
> relative to each other). and it is not extra work to have a stable sort,
> it is just a different algorithm. both the old sort (which you can still
> get via a pragma) and the new one are O(N log N) on average which means
> they are the same over all speed.
I stand corrected. I appreciate it.
Regards,
Brad
| |
| Tassilo v. Parseval 2004-04-21, 3:32 am |
| Also sprach Uri Guttman:
> and the promised module in that paper (5 years ago) is actually under
> full development right now and mostly working. it is the core of a talk
> i am giving at yapc::buffalo this june. the module is called Sort::Maker
> and this is what the OP would do to properly sort his rows of numbers
>
> use Sort::Maker ;
>
> my $sorter = make_sorter(
> plain => 1,
> number => '$_->[0]',
> number => '$_->[1]',
> number => '$_->[2]',
> ) ;
>
> my @sorted = $sorter->( @unsorted ) ;
>
> i may have an alpha version of this module ready in a few w s if
> anyone is interested in playing with it. the goal is to have it on cpan
> before yapc which is in mid-june.
Ah, the mythical sort module you've been telling us so often about is
eventually going to happen! ;-)
Nice! Its interface is quite .
Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
| |
| Uri Guttman 2004-04-21, 3:32 am |
| >>>>> "TvP" == Tassilo v Parseval <tassilo.parseval@rwth-aachen.de> writes:
[color=darkred]
TvP> Ah, the mythical sort module you've been telling us so often about is
TvP> eventually going to happen! ;-)
not mythical, but vaporific!
i actually started it way back then but after a viscious and well
deserved code review, i shelved it. and it took this many years before i
realized the major design flaw which was trying to have key code
extraction be described by some sick data structure. now i just make the
coder pass in the code. that removed over half the complexity. and also
i have damian helping me with the api and pod so it is much more fun. :)
TvP> Nice! Its interface is quite .
good to hear that. it was influenced (and it influenced) the sort stuff
that damian posted to the perl 6 lang list about 2 months ago.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
| |
| The King of Pots and Pans 2004-04-21, 3:32 am |
| Okay some of you asked for the code. Here it is. As you can tell I am
new to perl and am trying my best. I appreciate your kind help.
I cannot use any modules that does not come with the standard perl 5.6
install, as this program will be used on a number of different
machines with just the standard install.
--- code ---
#!/usr/bin/perl -w
use strict;
my @rows = ();
my @sorted = ();
open(DATA,"sort.txt") or die "cannot open file\n";
while(<DATA> )
{
my @fields = split /\s+/;
push @rows, \@fields; # list of refs to lists
}
@sorted = sort {$a->[1] <=> $b->[1]} @rows;
@sorted = sort {$a->[2] <=> $b->[2]} @rows;
for my $fields (@sorted)
{
print join " ", @$fields, "\n";
}
close(DATA);
--- code ---
sort.txt:
6 4 9
5 3 9
The first 'sort' command produces this:
5 3 9
6 4 9
This is correct because 3 < 4. The next 'sort' command produces this:
6 4 9
5 3 9
This is undesirable, as 9 = 9 so the order of the rows is desired to
remain the way it was in the previous sort with 3 < 4.
I hope this was more clear than my original post. I apologize for not
being so clear to begin with.
The data represented is just an example. There are actually many
columns and hundreds of rows.
--
The King of Pots and Pans
| |
| Joe Smith 2004-04-21, 4:33 am |
| The King of Pots and Pans wrote:
> Okay some of you asked for the code. Here it is. As you can tell I am
> new to perl and am trying my best. I appreciate your kind help.
>
> @sorted = sort {$a->[1] <=> $b->[1]} @rows;
> @sorted = sort {$a->[2] <=> $b->[2]} @rows;
There's your problem.
You need to make a single two-level sort, not two separate sorts.
@sorted = sort {$a->[1] <=> $b->[1] or $a->[2] <=> $b->[2]} @rows;
-Joe
| |
| René Larsen 2004-04-21, 6:32 am |
| In article <_%ohc.65863$U83.21047@fed1read03>, The King of Pots and Pans
wrote:
>
> Okay some of you asked for the code. Here it is. As you can tell I am
> new to perl and am trying my best. I appreciate your kind help.
>
> I cannot use any modules that does not come with the standard perl 5.6
> install, as this program will be used on a number of different
> machines with just the standard install.
>
> --- code ---
> #!/usr/bin/perl -w
>
> use strict;
>
> my @rows = ();
> my @sorted = ();
>
> open(DATA,"sort.txt") or die "cannot open file\n";
>
> while(<DATA> )
> {
> my @fields = split /\s+/;
> push @rows, \@fields; # list of refs to lists
> }
>
> @sorted = sort {$a->[1] <=> $b->[1]} @rows;
> @sorted = sort {$a->[2] <=> $b->[2]} @rows;
This is where you are making mistakes. You sort the data twice *but* you
only keep the last. You probably meant:
@sorted = sort {$a->[1] <=> $b->[1]} @rows;
@sorted = sort {$a->[2] <=> $b->[2]} @sorted;
But normally when sorting by more than one key, one sorts the *last* key
first:
@sorted = sort {$a->[2] <=> $b->[2]} @rows;
@sorted = sort {$a->[1] <=> $b->[1]} @sorted;
Try sorting your data on paper, and see what happens if you sort it from
left to right or right to left.
But, IMHO it would be better to sort it only once:
@sorted = sort {
$a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] || $a->[2] <=> $b->[2]
} @rows;
This cuts down on the comparisons, as you only test keys 2 if keys 1 are
equal, and so on.
Anorther thing: Your test data is not good. You should at least use
something like:
6 4 9
5 7 9
Regards, René
| |
| Uri Guttman 2004-04-21, 9:33 am |
| >>>>> "RL" == René Larsen <rene.larsen@spamfilter.dk> writes:
[color=darkred]
RL> This is where you are making mistakes. You sort the data twice *but* you
RL> only keep the last. You probably meant:
RL> @sorted = sort {$a->[1] <=> $b->[1]} @rows;
RL> @sorted = sort {$a->[2] <=> $b->[2]} @sorted;
RL> But normally when sorting by more than one key, one sorts the *last* key
RL> first:
RL> @sorted = sort {$a->[2] <=> $b->[2]} @rows;
RL> @sorted = sort {$a->[1] <=> $b->[1]} @sorted;
RL> Try sorting your data on paper, and see what happens if you sort it from
RL> left to right or right to left.
that will not be any better than his code. 2 separate sorts will not do it.
RL> But, IMHO it would be better to sort it only once:
RL> @sorted = sort {
RL> $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] || $a->[2] <=> $b->[2]
RL> } @rows;
this isn't better, it is correct. see my other post where i divined his
problem without even seeing any code. :)
RL> This cuts down on the comparisons, as you only test keys 2 if keys 1 are
RL> equal, and so on.
that has NOTHING to do with it. multikey sorts are not a speed up but a
CORRECY way to do sorting and subsorting. your first code and the OP's
code are both wrong, not slow.
RL> Anorther thing: Your test data is not good. You should at least use
RL> something like:
RL> 6 4 9
RL> 5 7 9
huh? his data was fine as it showed the problem. and your data isn't
good either as with three keys to sort on you should have even more
rows for testing.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
| |
| Jim Gibson 2004-04-21, 12:53 pm |
| In article <x78ygpo5si.fsf@mail.sysarch.com>, Uri Guttman
<uri@stemsystems.com> wrote:
>
>
> RL> This is where you are making mistakes. You sort the data twice *but*
> you
> RL> only keep the last. You probably meant:
>
> RL> @sorted = sort {$a->[1] <=> $b->[1]} @rows;
> RL> @sorted = sort {$a->[2] <=> $b->[2]} @sorted;
>
> RL> But normally when sorting by more than one key, one sorts the *last*
> key
> RL> first:
>
> RL> @sorted = sort {$a->[2] <=> $b->[2]} @rows;
> RL> @sorted = sort {$a->[1] <=> $b->[1]} @sorted;
>
> RL> Try sorting your data on paper, and see what happens if you sort it
> from
> RL> left to right or right to left.
>
> that will not be any better than his code. 2 separate sorts will not do it.
I am afraid that is incorrect. You can emulate a multi-column sort by
sorting on individual columns in multiple passes provided 1) you have a
stable sort (sequence-preserving) and 2) you sort in inverse order of
priority. Try it and see.
| |
| The King of Pots and Pans 2004-04-21, 12:53 pm |
| On Wed, 21 Apr 2004 at 12:48 GMT, Uri Guttman spoke:
> this isn't better, it is correct. see my other post where i divined
> his problem without even seeing any code. :)
I appreciate your insight in this matter. You appear to have extensive
education in this area.
For this problem I can only use the standard modules that come with
Perl 5.6. Is perl 5.6 up to the task? Or can the solution only be had
by downloading your module?
--
The King of Pots and Pans
| |
| Uri Guttman 2004-04-21, 2:45 pm |
| Xref: kermit comp.lang.perl.misc:315511
[color=darkred]
JG> In article <x78ygpo5si.fsf@mail.sysarch.com>, Uri Guttman
JG> <uri@stemsystems.com> wrote:
RL> But normally when sorting by more than one key, one sorts the *last*[color=darkred]
RL> first:[color=darkred]
RL> @sorted = sort {$a->[2] <=> $b->[2]} @rows;
RL> @sorted = sort {$a->[1] <=> $b->[1]} @sorted;
[color=darkred]
JG> I am afraid that is incorrect. You can emulate a multi-column sort
JG> by sorting on individual columns in multiple passes provided 1)
JG> you have a stable sort (sequence-preserving) and 2) you sort in
JG> inverse order of priority. Try it and see.
yes, that would work. and in older perls the sort function was not
stable (it is now). so that is not always a viable option. so a multikey
sort is still a better choice IMO.
uri
| |
| Paul Lalli 2004-04-21, 2:45 pm |
| On Wed, 21 Apr 2004, Jim Gibson wrote:
> In article <x78ygpo5si.fsf@mail.sysarch.com>, Uri Guttman
> <uri@stemsystems.com> wrote:
>
>
> I am afraid that is incorrect. You can emulate a multi-column sort by
> sorting on individual columns in multiple passes provided 1) you have a
> stable sort (sequence-preserving) and 2) you sort in inverse order of
> priority. Try it and see.
The OP has already stated that he only has Perl 5.6 available to him.
Perl's sort() did not become stable until v5.8.0
Paul Lalli
| |
| Uri Guttman 2004-04-21, 10:34 pm |
| >>>>> "TKoPaP" == The King of Pots and Pans <King@ask.for.email.invalid> writes:
TKoPaP> On Wed, 21 Apr 2004 at 12:48 GMT, Uri Guttman spoke:[color=darkred]
TKoPaP> I appreciate your insight in this matter. You appear to have extensive
TKoPaP> education in this area.
TKoPaP> For this problem I can only use the standard modules that come
TKoPaP> with Perl 5.6. Is perl 5.6 up to the task? Or can the solution
TKoPaP> only be had by downloading your module?
you can do a multikey sort with any perl. you can also do the multipass
sort mentioned in this thread. both require you to understand some more
about sorting in general but neither is that complicated. and you have
been given enough code and clues in this thread to solve your
problem. so hack it up in a small script and if you can't get to work,
post that code back here. but you have to do the homework first so study
the examples and ideas posted already.
uri
| |
| christie 2004-04-22, 3:45 pm |
| Uri Guttman <uri.guttman@fmr.com> wrote in message news:<sisc7jw95man.fsf@tripoli.fmr.com>...
>
> TKoPaP> On Wed, 21 Apr 2004 at 12:48 GMT, Uri Guttman spoke:
>
> TKoPaP> I appreciate your insight in this matter. You appear to have extensive
> TKoPaP> education in this area.
>
> TKoPaP> For this problem I can only use the standard modules that come
> TKoPaP> with Perl 5.6. Is perl 5.6 up to the task? Or can the solution
> TKoPaP> only be had by downloading your module?
>
> you can do a multikey sort with any perl. you can also do the multipass
> sort mentioned in this thread. both require you to understand some more
> about sorting in general but neither is that complicated. and you have
> been given enough code and clues in this thread to solve your
> problem. so hack it up in a small script and if you can't get to work,
> post that code back here. but you have to do the homework first so study
> the examples and ideas posted already.
>
> uri
========================================
=====================
Guys.... Have you heard simplicity is beauty. Don't make your life
more complited than it is.
Here is a hint
9 2 6
5 1 6
using split to differenciate first and second row.
push 9 2 &6 into arr_tmp1
push 5 1 &6 into arr_tmp2
push arr_tmp1[0] & arr_tmp2[0] into array1
push arr_tmp1[1] & arr_tmp2[1] into array2
push arr_tmp1[2] & arr_tmp2[2] into array3
Just sort the array{1-3} . Sort array{1-3} == sort column{1-3}
To display, well.... you know how to display it.
| |
| Uri Guttman 2004-04-22, 5:36 pm |
| >>>>> "c" == christie <kenvin007@yahoo.com> writes:
c> Guys.... Have you heard simplicity is beauty. Don't make your life
c> more complited than it is.
my life is very complite. :)
c> Here is a hint
oh boy! i need one of those!
c> 9 2 6
c> 5 1 6
c> using split to differenciate first and second row.
hmm, the OP already had those in an list of lists. why is split needed?
c> push 9 2 &6 into arr_tmp1
c> push 5 1 &6 into arr_tmp2
and what language is that written in?
c> push arr_tmp1[0] & arr_tmp2[0] into array1
c> push arr_tmp1[1] & arr_tmp2[1] into array2
c> push arr_tmp1[2] & arr_tmp2[2] into array3
and what language is that written in?
that isn't even legal pseudo-code! :)
c> Just sort the array{1-3} . Sort array{1-3} == sort column{1-3}
and how does that sort multiple columns?
and your working code is where?
and how do you extend that beyond 2 rows of 3 numbers each?
what are you talking about?
try a little more complication in your life. being correct is better
than being simple.
uri
| |
| Robin 2004-04-25, 10:52 pm |
|
"Uri Guttman" <uri.guttman@fmr.com> wrote in message
news:siscsmev4vec.fsf@tripoli.fmr.com...
>
> c> Guys.... Have you heard simplicity is beauty. Don't make your life
> c> more complited than it is.
>
> my life is very complite. :)
>
> c> Here is a hint
>
> oh boy! i need one of those!
>
> c> 9 2 6
> c> 5 1 6
>
> c> using split to differenciate first and second row.
>
> hmm, the OP already had those in an list of lists. why is split needed?
>
> c> push 9 2 &6 into arr_tmp1
> c> push 5 1 &6 into arr_tmp2
>
> and what language is that written in?
>
> c> push arr_tmp1[0] & arr_tmp2[0] into array1
> c> push arr_tmp1[1] & arr_tmp2[1] into array2
> c> push arr_tmp1[2] & arr_tmp2[2] into array3
>
> and what language is that written in?
>
> that isn't even legal pseudo-code! :)
>
> c> Just sort the array{1-3} . Sort array{1-3} == sort column{1-3}
>
> and how does that sort multiple columns?
>
> and your working code is where?
>
> and how do you extend that beyond 2 rows of 3 numbers each?
>
> what are you talking about?
>
> try a little more complication in your life. being correct is better
> than being simple.
>
> uri
uri, does my code...(below)...do what he needs?
Thanks,
-Robin
| |
|
|
"Uri Guttman" <uri.guttman@fmr.com> wrote in message
news:siscsmev4vec.fsf@tripoli.fmr.com...
>
> c> Guys.... Have you heard simplicity is beauty. Don't make your life
> c> more complited than it is.
>
> my life is very complite. :)
>
> c> Here is a hint
>
> oh boy! i need one of those!
>
> c> 9 2 6
> c> 5 1 6
>
> c> using split to differenciate first and second row.
>
> hmm, the OP already had those in an list of lists. why is split needed?
>
> c> push 9 2 &6 into arr_tmp1
> c> push 5 1 &6 into arr_tmp2
>
> and what language is that written in?
>
> c> push arr_tmp1[0] & arr_tmp2[0] into array1
> c> push arr_tmp1[1] & arr_tmp2[1] into array2
> c> push arr_tmp1[2] & arr_tmp2[2] into array3
>
> and what language is that written in?
>
> that isn't even legal pseudo-code! :)
>
> c> Just sort the array{1-3} . Sort array{1-3} == sort column{1-3}
>
> and how does that sort multiple columns?
>
> and your working code is where?
>
> and how do you extend that beyond 2 rows of 3 numbers each?
>
> what are you talking about?
>
> try a little more complication in your life. being correct is better
> than being simple.
>
> uri
uri, does my code...(below)...do what he needs?
Thanks,
-Robin
| |
| Tassilo v. Parseval 2004-04-27, 1:52 am |
| Also sprach Robin:
> "The King of Pots and Pans" <King@ask.for.email.invalid> wrote in message
> news:fdjhc.65339$U83.9813@fed1read03...
[color=darkred]
> haven't learned too much about pointers and references (my books suck), but
> I'd like to show you how to sort them if their truly seperated by spaces,
> you don't need a fancy sort routine. I saw your other code, and it was too
> fancy for such a simple operation. You should always post your code first.
>
> #!/usr/bin/perl
>
> $file = 'nums.txt';
>
> open (FILE, $file) or die "Error: $!";
> #flock it
> my (@array3, @array2, @array1, $tmp);
> @array1 = <FILE>;
> close (FILE);
> chomp (@array1);
> foreach (@array1)
> {
> @array2 = split (/ /);
> @array2 = sort (@array2);
> push (@array3, @array2);
> }
>
> print @array3;
I'm afraid that the specifications of the OP are slightly more
complicated. Given a file with these lines
6 5 4
3 2 1
your program turns this into the list
(4, 5, 6, 1, 2, 3)
The OP however wants to preserve the two dimensions of the table and
sort by the second and then by the third column. References are needed
for that.
To create the source array, the file can easily be read line-wise. No
slurping needed:
my @table;
while (<FILE> ) {
push @table, [ split ];
}
After that we have:
@table = ( [6, 5, 4],
[3, 2, 1], );
The square brackets denote an array reference. @array has two elements,
both being references to arrays of length 3.
And finally the sort:
my @sorted = sort { $a->[1] <=> $b->[1]
or
$a->[2] <=> $b->[2] } @table;
$a and $b will hold the array references as they are found in @table.
'$a->[1]' is the second element in the array referenced by $a.
The sort condition
$a->[1] <=> $b->[1]
or
$a->[2] <=> $b->[2]
first compares the two second elements of the two lines in $a and $b. If
those are equal, then (and only then) '<=>' will return 0 and the second
comparison (which compares the third element of the two lines) is
carried out.
> on a side note- what does this do??
> push (@array3, [@array2]);
This will populate a new (anonymous) array with the values in @array2
and push a reference to this array onto @array3. It's the same as
writing:
push @array3, [ split ];
As for references in Perl (note that Perl doesn't know about pointers),
see the corresponding parts of the perldocs. Maybe in this order:
perlreftut (tutorial on references)
perllol (list of lists)
perldsc (data structure cookbook)
perlref (all there is about references)
References in Perl are more essential and vital than in many other
languages, as there is no generic support for multi-dimensional data
structures. Instead this is all done through references. Hence they
should be tackled rather early.
Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
| |
| The King of Pots and Pans 2004-04-27, 1:53 am |
| On Sun, 25 Apr 2004 at 22:42 GMT, Robin spoke:
> "The King of Pots and Pans" <King@ask.for.email.invalid> wrote in message
> news:fdjhc.65339$U83.9813@fed1read03...
[color=darkred]
> haven't learned too much about pointers and references (my books suck), but
> I'd like to show you how to sort them if their truly seperated by spaces,
> you don't need a fancy sort routine. I saw your other code, and it
> was too fancy for such a simple operation. You should always post
> your code first.
Thanks for the insight. Those numbers in my original post were just an
example. The number of columns and rows are dynamic depending on which
file is loaded. They are not known values.
--
The King of Pots and Pans
| |
| Sherm Pendley 2004-04-27, 1:53 am |
| Robin wrote:
> haven't learned too much about pointers and references (my books suck),
If you have a book that tells you Perl has pointers, then it does indeed
suck beyond measure.
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
| |
|
|
>
but[color=darkred]
spaces,[color=darkred]
>
> Thanks for the insight. Those numbers in my original post were just an
> example. The number of columns and rows are dynamic depending on which
> file is loaded. They are not known values.
>
> --
> The King of Pots and Pans
tassilo actually pointed out that this code won't work, but it's a start,
and will create a list for a dynamic table of numbers.
-Robin
| |
|
|
"Sherm Pendley" <spamtrap@dot-app.org> wrote in message
news:p4SdnbuArKZFLxHdRVn-tA@adelphia.com...
> Robin wrote:
>
>
> If you have a book that tells you Perl has pointers, then it does indeed
> suck beyond measure.
my book, it said perl points to Pearls!
-Robin
| |
| Anno Siegel 2004-04-27, 10:01 am |
| Robin <robin @ infusedlight.net> wrote in comp.lang.perl.misc:
[50 lines by Uri snipped]
> uri, does my code...(below)...do what he needs?
What code? And how about testing your code yourself?
You should take a little more care in preparing your articles. Quoting
fifty lines just to add a single one is not the done thing. Forgetting
to add necessary text (or to remove large chunks of unwanted text) may
happen once in a while, but with your posts it happens with a regularity
that seems to say you don't care.
Take the time to look over your articles before you post them and make
sure they contain all you want them to contain and nothing you don't
want them to contain. On Usenet, that's part of common courtesy.
Anno
| |
|
| > What code? And how about testing your code yourself?
The code below, it's a sort routine. And your right, I should have
referenced it.
> You should take a little more care in preparing your articles. Quoting
> fifty lines just to add a single one is not the done thing. Forgetting
> to add necessary text (or to remove large chunks of unwanted text) may
> happen once in a while, but with your posts it happens with a regularity
> that seems to say you don't care.
> Take the time to look over your articles before you post them and make
> sure they contain all you want them to contain and nothing you don't
> want them to contain. On Usenet, that's part of common courtesy.
>
> Anno
Ok, good call.
-Robin
| |
|
| 7
"The King of Pots and Pans" <King@ask.for.email.invalid> wrote in message
news:fdjhc.65339$U83.9813@fed1read03...
> On disk I have a 2d table of numbers. Something like this:
>
> 9 8 6
> 4 5 6
>
> I read it in so that I have a list of references to lists. In other
> words, a list of the rows.
>
> I want to first sort by column 2, then column 3. When I sort by column
> 2 I get:
>
> 4 5 6
> 9 8 6
>
> As expected because 5 < 8. When I sort by column 3 I get:
>
> 9 8 6
> 4 5 6
>
> This is unexpected. Since 6 = 6, I don't want it to perform the
> sort. See how it was already sorted by column 2 when column 3 elements
> were equal, but now column 2 is unsorted again.
>
>
> --
> The King of Pots and Pans
haven't learned too much about pointers and references (my books suck), but
I'd like to show you how to sort them if their truly seperated by spaces,
you don't need a fancy sort routine. I saw your other code, and it was too
fancy for such a simple operation. You should always post your code first.
#!/usr/bin/perl
$file = 'nums.txt';
open (FILE, $file) or die "Error: $!";
#flock it
my (@array3, @array2, @array1, $tmp);
@array1 = <FILE>;
close (FILE);
chomp (@array1);
foreach (@array1)
{
@array2 = split (/ /);
@array2 = sort (@array2);
push (@array3, @array2);
}
print @array3;
on a side note- what does this do??
#!/usr/bin/perl
$file = 'nums.txt';
open (FILE, $file) or die "Error: $!";
#flock it
my (@array3, @array2, @array1, $tmp);
@array1 = <FILE>;
close (FILE);
chomp (@array1);
foreach (@array1)
{
@array2 = split (/ /);
@array2 = sort (@array2);
push (@array3, [@array2]);
}
print @array3;
--
Regards,
-Robin
--
[ webmaster @ infusedlight.net ]
www.infusedlight.net
--
| |
| Joe Smith 2004-05-04, 2:05 pm |
| Robin wrote:
>
> The code below, it's a sort routine.
Code? What code? (Robin forgets to copy-and-paste yet again.)
> And your right, I should have referenced it.
Are you sure you're right?
("your right" = "my left", "your left" = "my right")
| |
| David Combs 2004-05-21, 12:31 am |
| In article <x74qreozy9.fsf@mail.sysarch.com>,
Uri Guttman <uri@stemsystems.com> wrote:
....
>
>and the promised module in that paper (5 years ago) is actually under
>full development right now and mostly working. it is the core of a talk
>i am giving at yapc::buffalo this june. the module is called Sort::Maker
>and this is what the OP would do to properly sort his rows of numbers
>
> use Sort::Maker ;
>
> my $sorter = make_sorter(
> plain => 1,
> number => '$_->[0]',
> number => '$_->[1]',
> number => '$_->[2]',
> ) ;
>
> my @sorted = $sorter->( @unsorted ) ;
>
>i may have an alpha version of this module ready in a few w s if
>anyone is interested in playing with it. the goal is to have it on cpan
>before yapc which is in mid-june.
Yes, I'd very much to have a go at using it. Thus, please
add me to your list of email-addrs to send your
"buggy, but worth a try" announcement to.
Thanks!
David dkcombs@panix.com
| |
| Anno Siegel 2004-05-21, 5:31 am |
| Uri Guttman <uri@stemsystems.com> wrote in comp.lang.perl.misc:
> use Sort::Maker ;
>
> my $sorter = make_sorter(
> plain => 1,
> number => '$_->[0]',
> number => '$_->[1]',
> number => '$_->[2]',
> ) ;
>
> my @sorted = $sorter->( @unsorted ) ;
>
> i may have an alpha version of this module ready in a few w s if
> anyone is interested in playing with it. the goal is to have it on cpan
> before yapc which is in mid-june.
I assume you'll announce the alpha on clpm. I'm watching.
Anno
| |
| Uri Guttman 2004-05-21, 11:35 am |
| >>>>> "AS" == Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> writes:
AS> Uri Guttman <uri@stemsystems.com> wrote in comp.lang.perl.misc:[color=darkred]
AS> I assume you'll announce the alpha on clpm. I'm watching.
i gave a warmup talk on it the other night to boston.pm. you can get an
alpha tarball from
http://stemsystems.com/sort/Sort-Maker-0.01.tar.gz
the dir sort/slides/slides has the slide show. the module is in fairly
good shape but it needs more tests, benchmarks, examples (in pod and
slides), and some minor things/features are still to be done. volunteer
help is welcome and will be paid in acknowledgement glory. i should have
a newer alpha/beta up next w after i do an edit pass this w end. my
goal is to cpan it before yapc as i am giving the talk there.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
|
|
|
|
|