RE: passing an argument to a subroutine

2003-09-08 Thread Mike Harrison
Hi all,

I'm a bit late for a reply, but thought it would be appropriate to ask Babs
exactly what was required from the perl program.

Did you want to print the number of elements in the array, or print each
element in the array?

As Andrew Brosnan explained, setting a scalar equal to an array name will
result in the scalar containing the number of elements in the array:
eg.
$scalar = (1..5); # $scalar will be given the value of 5.

See my comments below...

Mike.

-Original Message-
From: B. Fongo [mailto:[EMAIL PROTECTED]
Sent: Thursday, 04 September, 2003 7:34 PM
To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
Subject: passing an argument to a subroutine


Hello

An argument passed to a subroutine returns wrong value.

Code example:

@x = (1..5);  [Mike: @x = (1,2,3,4,5)]
$x = @x;  [Mike: $x = 5, the number of elements in @x]

showValue ($x);   # or showValue (\$x);  [Mike: pass $x i.e. '5' to the
subroutine]


sub showValue {
  
  my $forwarded = @_;  [Mike: $forwarded = 'array passed' or $forwarded =
(5)]
[Mike: Note, this has set $forwarded equal to the number of elements in
array (5), or 1]
  print $forwarded;  # print ${$forwarded};  [Mike: print $forwarded now
prints '1']

}

In both cases, the script prints out 1.
What is going on here?

Thanks

Babs

[Mike:  If you want the subroutine to print the number of elements in the
array, you need to change the subroutine to:

sub showValue {
  my $forwarded = $_;
  print $forwarded;
}

That way you pass the scalar $x to the subroutine, which equals 5.
Then the subroutine gets the passed parameter and puts it in the $forwarded
scalar varable, then prints the scalar $forwarded as '5'.
]

If you want the values of the array to be printed, i.e. 1 2 3 4 5, then:

1. remove the line [EMAIL PROTECTED];
2. pass the array to the subroutine thus: showValue(@x);
3. You might need to format the printed output to separate values as it will
likely print '12345'.

Hope this helps,
Mike.


Re: passing an argument to a subroutine

2003-09-08 Thread R. Joseph Newton
"B. Fongo" wrote:

> Hello
>
> An argument passed to a subroutine returns wrong value.
>
> Code example:
>
> @x = (1..5);
> $x = @x;
>
> 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?

Nothing, at least not with the execution of your program.
Your print statement is doing exactly what you tell it to
do.  It is printing the value stored in $forwarded, which
is the number of items in the argument list.  Since you
called the function with one element, the number 5, this is
correct.

There is one very big problem with your code, though.  It
is missing something.

The time to:
use strict;
use warnings;
is with your first Perl program, and every program you
write thereafter.

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: passing an argument to a subroutine

2003-09-05 Thread Todd W.

"B. Fongo" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> Hello
>
> An argument passed to a subroutine returns wrong value.
>
> Code example:
>
> @x = (1..5);
> $x = @x;
>

here, $x gets the number of elements in @x

>
> showValue ($x);   # or showValue (\$x);
>
>
> sub showValue {
>
>   my $forwarded = @_;
>

$forwarded gets the number of elements in @_

>
>   print $forwarded;  # print ${$forwarded};
>
> }
>
> In both cases, the script prints out 1.
> What is going on here?
>

What do you expect to happen? What _is_ happening is exactly what is
supposed to.

Todd W.



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: passing an argument to a subroutine

2003-09-04 Thread drieux
On Thursday, Sep 4, 2003, at 04:53 US/Pacific, Andrew Brosnan wrote:
On 9/4/03 at 11:34 AM, [EMAIL PROTECTED] (B. Fongo) wrote:
An argument passed to a subroutine returns wrong value.

Code example:
[..]
In both cases, the script prints out 1.
What is going on here?
You are asking Perl for the number of elements in @_.

If you want the value(s) from @_, then
 my $forwarded = shift;
 or
 my ($forwarded) = @_;
there is also the minor defect that the sub

sub showValue {
  my ($forwarded) = @_;
print $forwarded;
}
will generate something like "ARRAY(0xdba8)"
which is the Reference - not the content - he
will most likely want to deal with what the reference passed, eg:

my @x =  (1..5);

print "#--\n";

showValue([EMAIL PROTECTED]);
print "\n#--\n# Calling the Pretty \n";
pretty_show_values([EMAIL PROTECTED]);

sub showValue {
  my $forwarded = shift;
  print @$forwarded;  # print ${$forwarded};
}

sub pretty_show_values {
  my $forwarded = shift;
  print "@$forwarded\n";  # print ${$forwarded};
}
also it is simpler to just 'dereference' the array directly.

ciao
drieux
---

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Re: passing an argument to a subroutine

2003-09-04 Thread Andrew Brosnan
On 9/4/03 at 11:34 AM, [EMAIL PROTECTED] (B. Fongo) wrote:

> Hello
> 
> An argument passed to a subroutine returns wrong value.
> 
> Code example:
> 
> @x = (1..5);
> $x = @x;
> 
> 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?

You are asking Perl for the number of elements in @_.

If you want the value(s) from @_, then
 my $forwarded = shift;
 or
 my ($forwarded) = @_;

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]