How do I call sort with an anonymous subroutine stored in a hash ??

Discussion in 'Perl' started by Casey, Jan 30, 2004.

  1. Casey

    Casey Guest

    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, Jan 30, 2004
    #1
    1. Advertising

  2. Casey

    Guest

    Casey <> wrote in message news:<>...
    > 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.
     
    , Jan 30, 2004
    #2
    1. Advertising

  3. Casey

    Anthony Guest

    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 <> wrote in message news:<>...
    > 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.
     
    Anthony, Jan 30, 2004
    #3
  4. Casey

    Casey Guest

    On Fri, 30 Jan 2004 04:03:25 -0800, nobull wrote:

    > Casey <> wrote in message news:<>...
    >> 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.
     
    Casey, Jan 30, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    556
    red floyd
    Nov 10, 2011
  2. Tore Aursand
    Replies:
    3
    Views:
    566
    Anno Siegel
    Sep 16, 2003
  3. florian
    Replies:
    9
    Views:
    124
    Michael Carman
    Apr 21, 2007
  4. king
    Replies:
    5
    Views:
    204
  5. Justin C
    Replies:
    1
    Views:
    195
    Justin C
    Oct 7, 2013
Loading...

Share This Page