If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Perl and the DBI > question regarding subroutine and sort function?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-21-07, 19:10
sjgrad03 sjgrad03 is offline
Registered User
 
Join Date: Aug 2003
Location: san jose, CA
Posts: 68
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
Reply With Quote
  #2 (permalink)  
Old 03-23-07, 11:52
sjgrad03 sjgrad03 is offline
Registered User
 
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;
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On