When I run your code (slightly modified) like so:
#!perl -w
[EMAIL PROTECTED] = param->('group');
@group = ("John","mark","Peter");
# let's @group contains "John, mark, Peter"
# I 'm passing exactly "John, mark, Peter" to the sub here.
do_db(@group);
sub do_db {
@x = @_;
# @x should be "John, mark, Peter" here too, but I get only John.
foreach (@x){
#insert into group_table (blah blah) values(blah blah);
print $_."\n";
}
}
using ActiveState ActivePerl on Windows 2000
I get 3 elements, one on each row. Nicely and clean.
I don't know what system you run it on but maybe there's a difference in our
dists of Perl?
What I would recomend you to do, either way, is to include use strict; and
declare the variables with my.
Just to make sure you don't overwrite variables.
That's what I can think of right now.
----- Original Message -----
From: "B. Fongo" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, September 04, 2003 12:32 PM
Subject: AW: passing an argument to a subroutine
I assigned it to scalar to get the number of elements contained in the
array @x.
Printing @x 0utside the subroutine gives me the right answer (5), but
within the sub, I get 1; which is wrong.
I need the correct number of elements within the sub to use it for a
database entry.
Why does Perl give me 5 elements outside the sub, but 1 within the sub?
This is exactly what my original sub does:
#!/usr/bin/perl -w
@group = param->('group');
# let's @group contains "John, mark, Peter"
# I 'm passing exactly "John, mark, Peter" to the sub here.
do_db(@group);
sub do_db {
@x = @_;
# @x should be "John, mark, Peter" here too, but I get only John.
foreach (@x){
insert into group_table (blah blah) values(blah blah);
}
}
-----Urspr�ngliche Nachricht-----
Von: Freddy S�derlund [mailto:[EMAIL PROTECTED]
Gesendet: Donnerstag, 4. September 2003 11:46
An: [EMAIL PROTECTED]
Betreff: Re: passing an argument to a subroutine
----- Original Message -----
From: "B. Fongo" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>
Sent: Thursday, September 04, 2003 11:34 AM
Subject: passing an argument to a subroutine
> Hello
>
> An argument passed to a subroutine returns wrong value.
What value do you want it to print?
>
> Code example:
>
> @x = (1..5);
> $x = @x;
Are you trying to get $x to contain "12345"?
Let's assume you want to pass the @x array into your subroutine and have
it
print out it's contents from there.
Then I think this is what you want.
#!perl -w
@x = (1..5);
showValue (@x);
sub showValue {
my @forwarded = @_;
foreach(@forwarded){
print "$_\n";
}
}
What it really comes to is what value you want to pass to your
subroutine.
>From reading your code, I can see that you are converting an array into
a
scalar but is that really what you want?
Some more explanation of what you would like to acomplish would be nice.
>
> showValue ($x); # or showValue (\$x);
>
>
> sub showValue {
>
> my $forwarded = @_;
> print $forwarded; # print ${$forwarded};
>
> }
>
> In both cases, the script prints out 1.
> What is going on here?
>
> Thanks
>
> Babs
>
--
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]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]