Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hash in a function 1

Status
Not open for further replies.

abruzzi

Programmer
Mar 27, 2007
17
FR
Hello ,


I've try but i've not arrived to make a hash in parameter in a function;

I've try this


Code:
sub test{

%values = @_;


}

Code:
sub test{

%values = %_;


}

but it's not run,
can you help me please;

Thanks everybody
 
Hi,
Send a hash parameter as a reference and dereference it in your sub.
Code:
test(\%myHash) ;

sub test
{
my $hashRef = shift ;
my $hashDeRef = %$hashRef ;
}


--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
this will work:


Code:
test(%hash);

sub test{
   %values = @_;
}

but only if the hash has all it's key/value pairs defind. The safer method is to do as spookie shows: use a reference.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
KevinADC said:
but only if the hash has all it's key/value pairs defind.

Hey Kevin,

Is that really true? From my understanding, having values that are undefined would not be a risk in that code at all.

In fact, I would argue that most of the time you probably want to use your method unless you specifically want the data structure to be modified, or if the data is inexorably large. Obviously, this is purely a coding style difference, but I like to ensure data integrity by isolating the structures from the caller to the sub from the very beginning.

- Miller
 
Well, unless this behavior is OK it's not a problem [wink]

Code:
%hash = (
   blah => test,
   foo => ,
   bar => test,
);

test(%hash);

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


output:

blah = test
test = 
foo = bar

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
oops, I see my example is a little funky but the output is still the same even if the code is all cleaned up and
"stict" is used.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
KevinADC said:
Well, unless this behavior is OK it's not a problem

That is not a problem with the sub calling method, that is a problem with the definition of the hash. It's already broken before the calling of the sub:

Code:
%hash = (
	blah => 'blahval',
	foo => ,
	bar => 'barval',
);

print "$_ = $hash{$_}\n" for keys %hash;


output:

blah = blahval
barval =
foo = bar

Whenever someone wants to do a definition like that, they should either use undef or the explicit empty string ''.

Code:
%hash = (
	blah => 'blahval',
	foo => undef,
	bar => 'barval',
);

test(%hash);

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


output:

blah = blahval
bar = barval
foo =

My personal preference is to just use the empty string. But its important to differentiate between a hash with undef values or just an improperly initialized hash.

- Miller
 
Yes, I did not mean to imply the calling method was not correct but I was not clear about that in my initial post. Your explanation has cleared it up. Have a well deserved star. [smile]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks for the star Kev.

I just wasn't sure if there was something I was misunderstanding from your earlier statement. If a hash isn't defined properly, it's not going to matter if you call the sub using a reference or a list.

I do sometimes wonder about the logic of perl ignoring consecutive commas.

Code:
@a = (1,,2,3,,,4,5);
# Equals (1,2,3,4,5);

I suppose the logic is that it is synonymous with joining empty arrays.

Code:
@a = (1),(),(2,3),(),(),(4,5);
# More obviously equals (1,2,3,4,5);

# Or easier to see.
@a = (1);
@empty = ();
@b = (2,3);
@c = (4,5);

@group = (@a,@empty,@b,@empty,@empty,@c);
# Equals (1,2,3,4,5);

Nevertheless, I have a slight problem when something that looks like an obvious and easy to detect syntax error is ignored by the language. I suppose that at some point you just gotta trust people not be so lazy that they introduce bugs that are so easy to avoid by being mindful.

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top