You need to pass the array to the function by reference, here's the fix.
&spin([EMAIL PROTECTED]);
sub spin
{
$arr = shift;
for (; $count > 0; $count--)
{
push(@$arr, $start++);
}
}
but if all you want to do is to populate your array with value between
1025 to 1035, here's a cleaner way.
@ports = (1025 .. 1025+10);
Tor.
David Newman wrote:
>
> Greetings. I have a newbie question about passing arrays into a subroutine
> (and getting return values as well).
>
> My script uses arrays for various values -- one array each for TCP port
> numbers, UDP port numbers, and the different bytes of IP addresses.
>
> Since I have to populate each of these arrays, I was hoping to be able to
> use one subroutine for all of them, just by feeding the subroutine the name
> of the array, the starting value, and the number of elements in the array.
>
> Unfortunately, the following doesn't work. I'm not even sure whether a
> subroutine can return an array.
>
> Thanks in advance for any pointers.
>
> Regards,
> David Newman
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my $count = 10;
> my @ports;
> my $start = 1025;
> &spin(@ports);
>
> sub spin {
> for (; $count > 0; $count--) {
> push (@_, $start++);
> }
> }
>
> foreach (@ports) {
> print "$_\n";
> }
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]