sort-like syntax

M

Matt Garrish

Peter J. Holzer said:
What version of perl are you using? perl 5.8.4 doesn't even compile the
program if you try to use sort inside a block with lexicals $a or $b:

my @x = qw(12 8 4 23 42 17 3);

my $a = 5;
my $b = 6;
my @y = sort {$a <=> $b} @x;
print "@y\n";

% perl foo2
Can't use "my $a" in sort comparison at foo2 line 5.

It depends on whether you declare $a and $b before or after the sort:

my @x = qw(12 8 4 23 42 17 3);

my @y = sort {$a <=> $b} @x;
my $a = 5;
my $b = 6;
print "@y\n";

% perl foo2
3 4 8 12 17 23 42

In this fellow's case, that's what I suspect he did, so it didn't affect his
sort but put his comparison into an endless loop.

Matt
 
B

Bart Lateur

Babacio said:
"Matt Garrish"
Could you please explain why you're trying to use the special
variables $a and $b outside of sorting? If you want to write bad
code you have to live with the consequences.

I was just wondering if it was possible to write a perl function
behaving like the built-in sort function.

I think it is easier for the user to write { $a <=> $b } than
{ $_[0] <=> $_[1] }.

Yes, there's even a widespread module that does that: List::Util, the
function reduce().

<http://search.cpan.org/perldoc?List::Util>
 
B

Babacio

"Matt Garrish"
That is not what you were asking, so please stick with your question. You
asked why you can't declare $a and $b and not have it affect the output.

Please read the orginal message. I did not asked that. I did not write
Mumia's code. I did not asked why declaring $a and $b affected the
output (I know why), I just *told* that it was the case and asked for
a solution to avoid it.
The subroutine in $code is global in scope
Yes.

time **stop trying to declare special variables**. Find some other
more meaningful name for your package variables. And if you don't
like being told your coding practices are bad, don't post bad code.

Please, read the code *I* posted, in the *first* message of the
thread. I think it is clean. If it is not clean, tell me where the
flaw is, I am interested. Strop attributing Mumia's code.
 
B

Babacio

"Matt Garrish"
Nope, I was right the first time. I forgot whether an autovivified
hash value would be converted to 0, but it appears not. Had you
turned on warnings in your code you should have discovered most of
what was going on on your own. Please bear that in mind...

I-WAS-NOT-AS-KING-THAT.
 
B

Babacio

Bart Lateur
Could you please explain why you're trying to use the special
variables $a and $b outside of sorting? If you want to write bad
code you have to live with the consequences.

I was just wondering if it was possible to write a perl function
behaving like the built-in sort function.

I think it is easier for the user to write { $a <=> $b } than
{ $_[0] <=> $_[1] }.

Yes, there's even a widespread module that does that: List::Util, the
function reduce().

I don't think reduce() can do what I want it to do, does it?
 
B

Babacio

"Peter J. Holzer"
What version of perl are you using? perl 5.8.4 doesn't even compile the
program if you try to use sort inside a block with lexicals $a or $b:

my @x = qw(12 8 4 23 42 17 3);

my $a = 5;
my $b = 6;
my @y = sort {$a <=> $b} @x;
print "@y\n";

% perl foo2
Can't use "my $a" in sort comparison at foo2 line 5.

Darn!
I was so sure it was OK that I did not try it!
(And I never went accross that problem, because I very seldom use $a
and $b as variable names).

OK, so the answer of my original question is something like: no, it is
impossible. Thanks a lot.
 
B

Babacio

Babacio
I don't think reduce() can do what I want it to do, does it?

I did not get your hint right. I can't shuffle by blocks with reduce,
but I can use the sort syntax, with variables $a and $b, yes. But if
one of these variables is declared before the call to reduce, it
screws up everything, without warnings!
 
B

Bart Lateur

Babacio said:

No, but it works using the special variables. You can use its
implementation as an example.
I did not get your hint right. I can't shuffle by blocks with reduce,
but I can use the sort syntax, with variables $a and $b, yes. But if
one of these variables is declared before the call to reduce, it
screws up everything, without warnings!

Simple rule of thumb: don't *ever* declare $a or $b lexical. Problem
solved. If you plan on using lexical variables, use other names.
 

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,776
Messages
2,569,603
Members
45,192
Latest member
KalaReid2

Latest Threads

Top