Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl (http://www.velocityreviews.com/forums/f17-perl.html)
-   -   How do I call sort with an anonymous subroutine stored in a hash ?? (http://www.velocityreviews.com/forums/t24803-how-do-i-call-sort-with-an-anonymous-subroutine-stored-in-a-hash.html)

Casey 01-30-2004 07:28 AM

How do I call sort with an anonymous subroutine stored in a hash ??
 
Hi, I haven't being using perl for too long. Can someone explain the
correct way to get the sort function to recognize an anonymous function
declared as a hash value? Look at my sample code for clarification:

#!/usr/bin/perl

@my_array = qw( g a z f u q m i e b );
$hash{my_sort_sub} = sub { $a cmp $b };
$hash{test_routine} = sub { print "test_routine works\n" };
&{$hash{test_routine}};
print @my_array;
print "\n";
print( sort &{$hash{my_sort_sub}} @my_array );
print "\n";


The code fails to compile with error:

Array found where operator expected at ./test.pl line 9, near "} "
(Missing operator before ?)
syntax error at ./test.pl line 9, near "} @my_array "
Execution of ./test.pl aborted due to compilation errors.

Help! I don't see what's wrong with this.


nobull@mail.com 01-30-2004 12:03 PM

Re: How do I call sort with an anonymous subroutine stored in a hash ??
 
Casey <mail@nowhere.com> wrote in message news:<pan.2004.01.30.07.28.21.56357@nowhere.com>.. .
> Can someone explain the
> correct way to get the sort function to recognize an anonymous function
> declared as a hash value?


AFAIK, you cannot. It's one of those nasty corners of Perl syntax
where to do the "right thing" would require unlimited lookahead (or
roll-back) in the parser. Perl doesn't even try - it's just
documented as a limitation.

> print( sort &{$hash{my_sort_sub}} @my_array );


my $sort_sub = $hash{my_sort_sub};
print( sort $sort_sub @my_array );

> Array found where operator expected at ./test.pl line 9, near "} "
> (Missing operator before ?)
> syntax error at ./test.pl line 9, near "} @my_array "


> Help! I don't see what's wrong with this.


The syntax of the Perl sort function is explained in

perldoc -f sort

This newsgroup does not exist (see FAQ). Please do not start threads
here.

Anthony 01-30-2004 01:40 PM

Re: How do I call sort with an anonymous subroutine stored in a hash ??
 
you are dereferencing the anonymous subroutine incorrectly.

On line 9 you have:
print( sort &{$hash{my_sort_sub}} @my_array );

It should be:
print( sort {&{$hash{my_sort_sub}}} @my_array );

hope this helps,
Anthony



Casey <mail@nowhere.com> wrote in message news:<pan.2004.01.30.07.28.21.56357@nowhere.com>.. .
> Hi, I haven't being using perl for too long. Can someone explain the
> correct way to get the sort function to recognize an anonymous function
> declared as a hash value? Look at my sample code for clarification:
>
> #!/usr/bin/perl
>
> @my_array = qw( g a z f u q m i e b );
> $hash{my_sort_sub} = sub { $a cmp $b };
> $hash{test_routine} = sub { print "test_routine works\n" };
> &{$hash{test_routine}};
> print @my_array;
> print "\n";
> print( sort &{$hash{my_sort_sub}} @my_array );
> print "\n";
>
>
> The code fails to compile with error:
>
> Array found where operator expected at ./test.pl line 9, near "} "
> (Missing operator before ?)
> syntax error at ./test.pl line 9, near "} @my_array "
> Execution of ./test.pl aborted due to compilation errors.
>
> Help! I don't see what's wrong with this.


Casey 01-30-2004 03:39 PM

Re: How do I call sort with an anonymous subroutine stored in a hash ??
 
On Fri, 30 Jan 2004 04:03:25 -0800, nobull wrote:

> Casey <mail@nowhere.com> wrote in message news:<pan.2004.01.30.07.28.21.56357@nowhere.com>.. .
>> Can someone explain the
>> correct way to get the sort function to recognize an anonymous function
>> declared as a hash value?

>
> AFAIK, you cannot. It's one of those nasty corners of Perl syntax
> where to do the "right thing" would require unlimited lookahead (or
> roll-back) in the parser. Perl doesn't even try - it's just
> documented as a limitation.
>
>> print( sort &{$hash{my_sort_sub}} @my_array );

>
> my $sort_sub = $hash{my_sort_sub};
> print( sort $sort_sub @my_array );
>
>> Array found where operator expected at ./test.pl line 9, near "} "
>> (Missing operator before ?)
>> syntax error at ./test.pl line 9, near "} @my_array "

>
>> Help! I don't see what's wrong with this.

>
> The syntax of the Perl sort function is explained in
>
> perldoc -f sort
>
> This newsgroup does not exist (see FAQ). Please do not start threads
> here.


Thanks for the solution. Have a good day.



All times are GMT. The time now is 08:37 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57