hash - Perl subroutine - return -
the next code create hash dynamically. if don't give return
statement in else
part of subroutine,
$var1 = { 'fruit' => { 'apple' => 'skin' } };
but if give return
statement
$var1 = { 'fruit' => { 'apple' => { 'red' => 'skin'} } };
which want. makes difference. can please educate me.
sub construct_hash{ ($hash, $value, $head, @tail ) = @_; if(@tail){ $hash = { $head => construct_hash(\%{$hash}, $value, shift @tail, @tail)} ; }else{ $hash->{$head} = $value; homecoming $hash; } } %h; @keys = qw (fruit apple red); $value = 'skin'; print dumper construct_hash(\%h, $value, shift @keys, @keys);
if don't specify homecoming subroutine, perl returns value of lastly look evaluated (see return). in case, means subroutine returns $hash->{$head}
in else
branch.
perl hash
No comments:
Post a Comment