On Tue, 2009-02-10 at 02:55 +0800, itshardtogetone wrote: > Hi, > Looking at the script below, can someone explain why the final output is > "Final = 1" instead of "Final = 5". > I thought at the end of the while loop, $a_ctr is 5, this value is then read > by the sub module &data() and this value of 5 is then passed on to sub > publish before its being printed out. Thanks
The data() sub sets $a_ctr equal to an array containing one value. The first time, the array is (1), the second time it is (2), and so on. This is done five times, each time setting $a_ctr to an array containing one value. At the end, the array contains one value (5) but when you print that value in scalar context, you get a "1" because that's the number of elements in the array. You will achieve the results you're looking for by doing this: $a_ctr = shift(@_); or, simply: $a_ctr = shift; If you want to pass additional variables to data(), write it this way: my ($a_ctr, $b_ctr, $c_ctr) = @_; and call it like this: data($a_ctr, $b_ctr, $c_ctr); What is an a_ctr, anyway? :) Passing variables to subroutines is something that's very hard (IMHO) to get used to if you first learned how to do it in languages like Java, or in my case, Ada. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/