Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2003
    Location
    san jose, CA
    Posts
    68

    Unanswered: question regarding subroutine and sort function?

    Hello everyone:

    I have a question regarding how to use a subroutine to sort an array contains a list of strings
    in this case country names.

    The requirement is to pass array to subroutine by using hard reference(pointer)

    I was able to passing pointer to the subroutine, but I can not figure out how
    to sort the array alphabetically inside the subroutine and return sorted array and print newly sorted array out. Please give me some advises and suggestions.

    use strict;
    use warnings;

    my @country = ("Russia", "China", "India", "Cuba");
    my $arrayptr = \@country;
    &sort_array($arrayptr);

    sub sort_array
    {
    my($array_ptr) = @_;
    sort (@$array_ptr); #line 24
    }

    I got error message says, "Useless use of sort in void context" at line 24

    Jeff
    3-21-07

  2. #2
    Join Date
    Aug 2003
    Location
    san jose, CA
    Posts
    68

    i resovled the problem

    sub sort_array
    {
    my($array_ptr) = @_;
    sort (@$array_ptr); #line 24
    }

    I found cause of the error. Becuase there was no capture for the return (sorted array). line 24 should be @$array_ptr = sort (@$array_ptr).

    If I want print out of the sorted array outside the subroutine.
    @sorted_array = &sort_array($arrayptr);
    print @sorted_array;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •