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:
need help with returning list from subroutine

 



leviculus
New User

Jul 8, 2014, 4:57 PM

Post #1 of 6 (1115 views)
need help with returning list from subroutine Can't Post

hi, i am trying to find a mistake i make with passing back the list from subroutine which divides input numbers by 2. i can print expected output from subroutine in library but fail to print the list from the main script after passing it back. here is one of the latest attempts to solve it, appreciate your input.

main script:

require 'test-lib.pl';
$line = <STDIN>;
my $values = split(?|s+/,$line); ##getting user's input #numbers
@num = half (@values); #calling subroutine, pass values
print "@num\n"; #getting values back from subroutine

test-lib.pl library with subroutine:

sub half {
my @array = @_;
foreach $item (@array) {
push (@array, $item); #push to the list
$item *=0.5; #divide all passed numbers by 2
}
return @array;
}


Zhris
Enthusiast

Jul 8, 2014, 5:45 PM

Post #2 of 6 (1110 views)
Re: [leviculus] need help with returning list from subroutine [In reply to] Can't Post

Hi,

Here are some issues I picked up on:

1) you should always use strict and warnings. This means you'll also have to ensure you declare all your variables with the likes of my.

2) not necessary but you might want to chomp your input to ensure the newline on the end is removed i.e. chomp(my $line = <STDIN>);. Either way, this knowledge is useful for the future.

3) your split regexp ?|s+/ is invalid. You probably meant /\s+/, or better ' '.

4) you assign the result of your split to a scalar which will hold the number of elements returned. You meant to assign to an array, specifically @values.

5) I don't understand your intention of doing push (@array, $item); in your half function. Note that when you modify the number of elements of the array you are looping, you will cause a segmentation fault. Any adjustments to $item will directly be reflected in @array, therefore remove the push line.

Hope these help.

And just for fun:


Code
$, = "\n"; 
print map { $_/2 } split ' ', <STDIN>;


Chris


(This post was edited by Zhris on Jul 8, 2014, 6:04 PM)


leviculus
New User

Jul 8, 2014, 6:37 PM

Post #3 of 6 (1097 views)
Re: [Zhris] need help with returning list from subroutine [In reply to] Can't Post

thanks, yes the ? and @values are typos, and i fail to print from main script not because of that... the objective was not to use map function. i tried with returning return scalar and returning array and all i could get back in main script was first value divided by 2, thats why i thought to use push to get the whole list... i think the problem is with returning the $item back as a list but seems not able to find the way to pass the whole list


Zhris
Enthusiast

Jul 8, 2014, 6:48 PM

Post #4 of 6 (1094 views)
Re: [leviculus] need help with returning list from subroutine [In reply to] Can't Post

Hi,

Could you re-post your script with the corrections you have made. For convenience, could you possibly temporarily put your subroutine half into your main script so that your code can be tested standalone.


Quote
i tried with returning return scalar and returning array and all i could get back in main script was first value divided by 2


I don't quite understand what you mean, but there is a possibility that it was a coincidence. For example, if you provided a string of 2 numbers, and the first number in the string was 4, then the call to half() in scalar context would result in 2 (not half of 4, but 2 elements in the list).

Regards,

Chris


(This post was edited by Zhris on Jul 8, 2014, 6:50 PM)


Laurent_R
Veteran / Moderator

Jul 9, 2014, 10:01 AM

Post #5 of 6 (790 views)
Re: [leviculus] need help with returning list from subroutine [In reply to] Can't Post

A short try quickly typed directly on the command line:


Code
$perl -e 'use strict; use warnings; 
> my @arr = qw / 4 6 10 6 8 /;
> sub half {
> my @val = @_;
> $_ = $_/2 for @val;
> return @val;
> }
> my @num = half(@arr);
> print "@num \n";
> '
2 3 5 3 4



BillKSmith
Veteran

Jul 9, 2014, 10:54 AM

Post #6 of 6 (756 views)
Re: [leviculus] need help with returning list from subroutine [In reply to] Can't Post

The code in your original post should do what you expect if you fix the typos in the 'split' statement and delete the inappropriate push statement.

my $values = split(?|s+/,$line); ##getting user's input #numbers

my @values = split(/\s+/,$line); ##getting user's input #numbers

Chris and Laurent have suggested a number of improvements. Please consider all of them. (Use map inside your half.)
Good Luck,
Bill

 
 


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

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