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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Casey

    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_s ub} = sub { $a cmp $b };
    $hash{test_rout ine} = sub { print "test_routi ne works\n" };
    &{$hash{test_ro utine}};
    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

    #2
    Re: How do I call sort with an anonymous subroutine stored in a hash ??

    Casey <mail@nowhere.c om> wrote in message news:<pan.2004. 01.30.07.28.21. 56357@nowhere.c om>...[color=blue]
    > Can someone explain the
    > correct way to get the sort function to recognize an anonymous function
    > declared as a hash value?[/color]

    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.
    [color=blue]
    > print( sort &{$hash{my_sort _sub}} @my_array );[/color]

    my $sort_sub = $hash{my_sort_s ub};
    print( sort $sort_sub @my_array );
    [color=blue]
    > Array found where operator expected at ./test.pl line 9, near "} "
    > (Missing operator before ?)
    > syntax error at ./test.pl line 9, near "} @my_array "[/color]
    [color=blue]
    > Help! I don't see what's wrong with this.[/color]

    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.

    Comment

    • Anthony

      #3
      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_sor t_sub}}} @my_array );

      hope this helps,
      Anthony



      Casey <mail@nowhere.c om> wrote in message news:<pan.2004. 01.30.07.28.21. 56357@nowhere.c om>...[color=blue]
      > 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_s ub} = sub { $a cmp $b };
      > $hash{test_rout ine} = sub { print "test_routi ne works\n" };
      > &{$hash{test_ro utine}};
      > 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.[/color]

      Comment

      • Casey

        #4
        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:
        [color=blue]
        > Casey <mail@nowhere.c om> wrote in message news:<pan.2004. 01.30.07.28.21. 56357@nowhere.c om>...[color=green]
        >> Can someone explain the
        >> correct way to get the sort function to recognize an anonymous function
        >> declared as a hash value?[/color]
        >
        > 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.
        >[color=green]
        >> print( sort &{$hash{my_sort _sub}} @my_array );[/color]
        >
        > my $sort_sub = $hash{my_sort_s ub};
        > print( sort $sort_sub @my_array );
        >[color=green]
        >> Array found where operator expected at ./test.pl line 9, near "} "
        >> (Missing operator before ?)
        >> syntax error at ./test.pl line 9, near "} @my_array "[/color]
        >[color=green]
        >> Help! I don't see what's wrong with this.[/color]
        >
        > 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.[/color]

        Thanks for the solution. Have a good day.

        Comment

        Working...