Thursday, September 10, 2009

Playing with PERL Hash

simple PERL script utk menunjukkan bagaimana bermain dgn Hash. cara pass hash ke sub/class/function dan cara retrive back value dari parameter/variable yg di terima. simple tapi cukup rasanya menerangkan pengunaan hash. amat berguna.

# init
my %t = (
 1=>'k',
 2=>'l'
);

foreach my $f(keys %t) {
 print $f,'=', $t{$f}, "\n";
}
print "\n";

# process
my $t = &passHash(\%t);

foreach my $f(keys %$t) {
 print $f,'=', $t->{$f}, "\n";
}
print "\n";

# copy
my %k = %$t;
foreach my $f(keys %k) {
 print $f,'=', $k{$f}, "\n";
}
print "\n";


sub passHash {
 my $r = shift;
 $r->{3} = 'new';
 return \%$r;
}

No comments: