Re: UnPerl-like Code
by japhy (Canon) on Sep 28, 2001 at 00:41 UTC
|
Well, the my notwithstanding, that's how we do it in Perl... I mean, unless you can capture the list beforehand (and so be sorting something other than a named array), that's how to sort an array in Perl. There's no current in-place mechanism, although you could write it easily:
sub simple_sort {
@_[0 .. $#_] = sort @_;
}
# and
sub complex_sort (&@) {
my $c = shift;
@_[0 .. $#_] = sort $c @_;
}
_____________________________________________________
Jeff[japhy]Pinyan:
Perl,
regex,
and perl
hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??; | [reply] [d/l] |
Re: UnPerl-like Code
by dragonchild (Archbishop) on Sep 28, 2001 at 00:46 UTC
|
Ok. That code makes no sense. Let's disect it:
- my VAR ... This creates a lexical variable within the innermost scope you're in. In this case, it creates an array called @d.
- = ... assignment operator (but, you knew that)
- sort LIST ... Takes a list (or array which it converts to a list, but that's unimportant) which can be named or unnamed. In this case, it takes a list called @d, which has to have been defined earlier (at least, for it to make sense).
Now, you can do something like @d = sort @d; which basically means that you want to do an ASCII sort the values within @d and then store the result within @d. But, by having the my there, it doesn't make any sense.
If you put -w on your shebang line, you'd see a redefinition of @d warning.
As for it being unPerl-like, it's not. You may be used to a lot of in-place operators, and that's not C-like. But, sort doesn't work in-place, primarily because you often want to sort an array, then put the sorted result into another array.
------ We are the carpenters and bricklayers of the Information Age. Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement. | [reply] [d/l] |
Re: UnPerl-like Code
by Masem (Monsignor) on Sep 28, 2001 at 00:43 UTC
|
Until perl 6, you'll have to keep it like this. Last I looked, there's going to be a new operator akin to ~= that basically uses the left hand list as the argument to the right hand side, and then set the result of the right hand side to the left hand side, such that you can do: @d ?= sort; (it's not ?=, just as an example of the simplicity of the code)
-----------------------------------------------------
Dr. Michael K. Neylon - mneylon-pm@masemware.com
||
"You've left the lens cap of your mind on again, Pinky" - The Brain
It's not what you know, but knowing how to find it if you don't know that's important
| [reply] [d/l] |
Re: UnPerl-like Code
by merlyn (Sage) on Sep 28, 2001 at 03:19 UTC
|
my @d=sort(@d);
I just double-checked. We have nothing exactly like that in the llama.
Please be careful when you say something is "straight from the Llama's mouth".
It's nearly libelous if it's derogatory.
-- Randal L. Schwartz, Perl hacker
| [reply] [d/l] |
|
|
My apologies, the code I posted was not "straight from the Llama's mouth" and I should be more careful in using that phrase. The code which I was pondering is on page 55 of the 2nd edition of the Llama and reads as follows:
@y = sort(@y);
What caught my mind is that Perl makes a point of not having one repeat variables on both sides of an = and sorting an array while maintaining its name is the opposite. With humble contrition,
jg
| [reply] [d/l] |
(crazyinsomniac) Re: UnPerl-like Code
by crazyinsomniac (Prior) on Sep 28, 2001 at 04:04 UTC
|
#!/usr/bin/perl -w
use strict;
my @d = qw,I am stoned,;
{
my @d = sort @d;
printf "Q: %s?\n","@d";
}
printf "A: %s.\n","@d";
__END__
# The output is (as it should be)
Q:I am stoned?
A:I am stoned.
Now I don't really care if it shows up in some book, or whether or not it's unperl-like (whatever you think that means, T.I.M.T.O.W.T.D.I.!)
Basically what it comes down to is that you have to turn on strict and warnings, and revisit perlsyn and perlfunc, try things out before posting questions.
___crazyinsomniac_______________________________________
Disclaimer: Don't blame. It came from inside the void
perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;" | [reply] [d/l] |
Re: UnPerl-like Code
by tachyon (Chancellor) on Sep 28, 2001 at 00:51 UTC
|
sort() is an inbuilt function. It returns a sorted version of the array you pass it. If you want to keep that sorted array you need to assign it to an array. This is a lazy way to do it!
When you say sort @d you are actually saying sort { $a cmp $b } @d where $a and $b are magical vars that hold sequential pairs of elements in the sort. You use cmp to get an alphabetic sort and <=> to get a numeric one. If you want reverse the sort order you transpose the $a and $b. You can have very complex code in the block preceeding the sort because you have access to the sequential pairs in $a and $b. The default is { $a cmp $b } which is an ascending order aphabetic sort.
If you want wild, wicked and short look into grep and map in perlman:perlfunc.
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
| [reply] |
Re: UnPerl-like Code
by Flame (Deacon) on Sep 28, 2001 at 00:42 UTC
|
Hmm, is it just me, or does that not quite look right? Mabey I'm looking at it out of context, but you usually don't want to declare it with 'my' and use it at the same time, that implies that it already has a value. I think I've had perl complain at me more than once for attempting to 'my' a variable that already existed. (And complain at me for forgeting to my a variable when it comes into existance under strict...)
I could be mistaken though.
"Wierd things happen, get used to it"
Flame ~ Lead Programmer: GMS
http://gms.uoe.org
| [reply] |
Re: UnPerl-like Code
by stefp (Vicar) on Sep 28, 2001 at 00:46 UTC
|
@d=sort(@d); unperl-like since the var is named on both sides.
I suppose that you point is that you feel it is too verbose.
But sorting in place would be difficult for a list because
it is by definition read-only.
Also you may want to return a sorted list of the elements
of an array without changing the array itself.
| [reply] [d/l] |