sort numeric lists

  • Thread starter The King of Pots and Pans
  • Start date
R

Robin

7
The King of Pots and Pans said:
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.

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;
 
R

Robin

Uri Guttman said:
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
 
T

Tassilo v. Parseval

Also sprach Robin:
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
 
T

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.

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.
 
S

Sherm Pendley

Robin said:
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--
 
R

Robin

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.

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
 
R

Robin

Sherm Pendley said:
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
 
A

Anno Siegel

[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
 
R

Robin

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
 
J

Joe Smith

Robin said:
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")
 
D

David Combs

Uri Guttman said:
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 weeks 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 (e-mail address removed)
 
A

Anno Siegel

Uri Guttman said:
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 weeks 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
 
U

Uri Guttman

AS> Uri Guttman said:
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 weeks if
anyone is interested in playing with it. the goal is to have it on cpan
before yapc which is in mid-june.

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 week after i do an edit pass this weekend. my
goal is to cpan it before yapc as i am giving the talk there.

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,294
Latest member
LandonPigo

Latest Threads

Top