question about sort()

W

Wiseguy

OK. I need to sort a list and I have the following code

@L=(35,10,0,27,100,-4);
sub numeric { $A <=> $b; }
@L=sort numeric @L;

The only reference I have is an ancient Oreilly book on Perl 4.
does sort() actually sort the operand list or only return a sorted list?
Is it safe to use the source operand as the return value of sort() as well?
 
J

Jim Gibson

Wiseguy <[email protected]> said:
OK. I need to sort a list and I have the following code

@L=(35,10,0,27,100,-4);
sub numeric { $A <=> $b; }
@L=sort numeric @L;

The only reference I have is an ancient Oreilly book on Perl 4.
does sort() actually sort the operand list or only return a sorted list?
Is it safe to use the source operand as the return value of sort() as well?

One way to find out is try it. If you run the above code, you will get
some peculiar results. If you want to know why, put 'use strict;' at
the beginning of your program.

Another way is to read the documentation that comes with Perl
installations:

perldoc -f sort

although in this case that documentation does not explicitly say that
it sorts or doesn't sort the original array. So I refer you to method
1.

There are newer O'Reilly books on Perl. The curre

Good luck.

FYI: this newsgroup is defunct. Try comp.lang.perl.misc in the future
(but be sure to read the guidelines for that group before posting, as
you have violated more than one of the strictures for posting to that
group).
 
M

mazzawi

it returns a sorted array, it doesn't modify the source

#!/usr/bin/perl -w
use strict;
my @L=(35,10,0,27,100,-4);
sub numeric { $a <=> $b; }
@L = sort numeric @L;
print @L;
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top