CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
The best way to check array returned by subroutine

 



alejit0
New User

Oct 7, 2007, 5:12 AM

Post #1 of 2 (573 views)
The best way to check array returned by subroutine Can't Post

Hi. I wonder what is the best way to check is array returned by subroutine defined. For example:
I have subroutine that check is there in her arguments array any numbers greater than 100 and if so create array with that numbers. If not subroutine return 0.

Code
sub test { 
my (@result);
foreach (@_){
push (@result, $_) if ($_ > 100);
}
return @result if (@result);
return 0;
}

After this, in main program i want to assign result of that subroutine to any array, but if there 0 returned it will be assigned to array too, as I mean.
For example: @m_array = test(@any_other_arr);
If 0 returned @m_array contains only one element.
So how to check is returned array of numbers or just zero value. I used numbers only for example. it's maybe strings or letters or something else.
Sorry my English and thank you.


KevinR
Veteran


Oct 7, 2007, 10:20 AM

Post #2 of 2 (568 views)
Re: [alejit0] The best way to check array returned by subroutine [In reply to] Can't Post

something like this should work:


Code
my @array = (20, 30, 40, 10); 
my @v = test( @array );
if ( @v) {
print "@v";
}

sub test {
my @result = grep {$_ > 100} @_;
return @result ? @result : ();
}

-------------------------------------------------


(This post was edited by KevinR on Oct 7, 2007, 10:21 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives