Hash as an function argument

L

Lepi

Hello

When I'm calling a subroutine with one or two aguments, I use
something like this:
my ($first,$second)=@_;
to access them.

What if I want to pass a hash to function like:
%something={$first=>1,$second=>2};
function(%something);

sub function
{
How to access %something, and not to make it global???
}


Maybe I'm going in a totally wrong direction..

Thanks

Lepi
 
G

Gunnar Hjalmarsson

Lepi said:
When I'm calling a subroutine with one or two aguments, I use
something like this:
my ($first,$second)=@_;
to access them.

What if I want to pass a hash to function like:
%something={$first=>1,$second=>2};

Please be more careful with what you post to the group. That makes
absolutely no sense.

- Enable strictures and warnings, not least when experimenting with
Perl code.

- Copy, don't re-type, code that you post.

You should use parentheses, not braces, when assigning a hash in Perl.
function(%something);

sub function
{
How to access %something, and not to make it global???
}

What have you tried? As long as you only pass one hash, you can do
just like that. Then you don't actually pass a hash, but rather a list
of keys and values, but that does not prevent you from doing:

sub function {
my %something = @_;

However, you'd better get into the habit to pass a hash reference instead:

function( \%something );

sub function {
my $hashref = shift;
print "$_ = $hashref->{$_}\n" for keys %$hashref;
}
 
A

A. Sinan Unur

Hello

When I'm calling a subroutine with one or two aguments, I use
something like this:
my ($first,$second)=@_;
to access them.

What if I want to pass a hash to function like:
%something={$first=>1,$second=>2};

You seem to be a more than a little confused here about variables versus
hash keys and hashes versus hash references. Have you read perlsub,
perlreftut etc?

use strict;
use warnings;

sub dump_hash {
my %hash = @_;
for (keys %hash) {
print "\$hash{$_} = $hash{$_}\n" ;
}
}

sub dump_hash_ref {
my $hash_ref = shift;
for (keys %$hash_ref) {
print "\$hash_ref->{$_} = ", $hash_ref->{$_}, "\n";
}
}

dump_hash(first => 'One', second => 'Two');
dump_hash_ref({ first => 'One', second => 'Two' });

__END__
 
T

Tad McClellan

%something={$first=>1,$second=>2};


Your hash has a *single* pair in it.

The key is a stringified reference, its value is undef.

The values for $first and $second cannot be accessed at all.



That data structure is useless...

.... and so is this followup (which is to be expected when you
post so carelessly).
 
E

Eric Bohlman

call the following subroutine like this:
&example1(arg1 => value1, arg2 => value2, arg3 => value3);
sub example1 { my(%args) = @_; ... }

Thanks to the fact that if there are duplicate keys in a list used to
initialize a hash the last one "wins," you can supply default values for
some or all arguments to make them optional:

sub example1 {
my %args=(arg2=>default2, arg3=>default3, @_);
...
}

That would be quite useful if the sub were called most of the time with
different values for arg1 but the same value for arg2 or arg3 or both. In
the most common cases, you'd only need to supply one argument.
 

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

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,430
Latest member
7dog123

Latest Threads

Top