parameter error during function call

S

Slickuser

How do I fix this. I need to pass in as scalar, hash, array.

Thanks.

Odd number of elements in hash assignment at
use strict;
use warnings;

my %chars_G =
(
'a' => 'x',
'b' => 'y',
'c' => 'z',
'd' => 'a1',
'e' => 'a2',
'f' => 'a3'
);

my @info_G = (1,2,3,4,5);


show_name("XYZ",%chars_G, @info_G);

sub show_name
{
my ($name,%chars,@info) = @_;

}
 
M

Mirco Wahab

Slickuser said:
How do I fix this. I need to pass in as scalar, hash, array.

Odd number of elements in hash assignment at
...
show_name("XYZ",%chars_G, @info_G);

sub show_name
{
my ($name,%chars,@info) = @_;
...

Use 'call by reference' if you want to
avoid exactly that:

....
my %chars_G = ( a => 'x',
b => 'y',
c => 'z',
d => 'a1',
e => 'a2',
f => 'a3'
);
my @info_G = qw{ 1 2 3 4 5 };

# use 'call by reference \... ' in order to
# avoid 'subsequent list flattening'
show_name("XYZ", \%chars_G, \@info_G);
# if called with array/hash parameters 'by value'
# their elements get translated into one *single list*
# without any distinction regarding to their origin

sub show_name {
my ($name, $chars_by_reference, $info_by_reference) = @_;
my %chars_by_value = %{ $chars_by_reference }; # make local copy
my @info_by_value = @{ $info_by_reference }; # make local copy

print "@info_by_value\n";
print "$_ ==> $chars_by_value{$_}\n" for sort keys %chars_by_value;
}
....


Regards

M.
 
J

Jürgen Exner

Slickuser said:
How do I fix this. I need to pass in as scalar, hash, array.

Odd number of elements in hash assignment at
use strict;
use warnings;

my %chars_G =
(
'a' => 'x',
'b' => 'y',
'c' => 'z',
'd' => 'a1',
'e' => 'a2',
'f' => 'a3'
);

my @info_G = (1,2,3,4,5);


show_name("XYZ",%chars_G, @info_G);

Arguments are flattened, i.e. you are passing a total of 1+12+5 = 18
arguments.

See "perldoc perlsub", second paragraph, first sentence.
sub show_name
{
my ($name,%chars,@info) = @_;

You are extracting 1+17 parameters, because %chars will slurp up all of
the rest, leaving none for @info. And 17 looks like an odd number to me,
so the error message is correct.
How do I fix this. I need to pass in as scalar, hash, array.

See "perldoc perlsub", second paragraph, second sentence.

jue
 
P

Peter J. Holzer

Slickuser said:
How do I fix this. I need to pass in as scalar, hash, array.

Odd number of elements in hash assignment at
...
show_name("XYZ",%chars_G, @info_G);

sub show_name
{
my ($name,%chars,@info) = @_;
...

Use 'call by reference' if you want to
avoid exactly that: [...]
show_name("XYZ", \%chars_G, \@info_G);

This is NOT "call by reference". You are passing a reference
explicitely. "Call by reference" is one of the ways a language can pass
parameters into a subroutine. In fact it is the one perl uses:

#!/usr/bin/perl
my $x = 5;
my $y = 6;

foo($x, $y);
print "$x, $y\n";

sub foo {
$_[0] = 3;
}
__END__

prints
3, 6

I.e, the subroutine parameters (in @_) contain references (in Perl
jargon: aliases) to $x and $y, and by modifying them the arguments are
modified.

The opposite is "call by value" as in C, where the subroutine receives a
copy of the arguments.

(I'll skip "call by name" as in Algol here).

hp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,211
Messages
2,571,100
Members
47,695
Latest member
KayleneBee

Latest Threads

Top