For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > September 2007 > How print a Hashtable with reference ?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How print a Hashtable with reference ?
Santana

2007-09-22, 7:00 pm

Hei all,
hoe a print a hashtable elements in a function that receives the
reference of
this hastable ???


In this example this foreach loop in "printHT" function dont work ,
how is missed ?

#!/usr/bin/perl
use strict;
use warnings;

sub printHT($)
{
my $T =$_[0];

foreach my $id (keys (%$T)){ #This dont work :)

print $$T{$id} . "\n";
}

}


my %ht_state=("AL" => "Alabama","AK" => "Alaska");
&printHT(\%ht_state);

Phil

2007-09-23, 6:59 pm

The following should work for you:


#!/usr/bin/perl

use strict;
use warnings;

sub printHT {

my $T = shift;

foreach (keys (%$T)) {
print "$_ = $T->{$_}\n"; # to get Key = Value
print "$T->{$_}\n"; to just get the value
}

}

my %ht_state=(AL => "Alabama", AK => "Alaska");

printHT(\%ht_state);





On Sep 21, 3:55 am, paulito.sant...@gmail.com (Santana) wrote:
> Hei all,
> hoe a print a hashtable elements in a function that receives the
> reference of
> this hastable ???
>
> In this example this foreach loop in "printHT" function dont work ,
> how is missed ?
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> sub printHT($)
> {
> my $T =$_[0];
>
> foreach my $id (keys (%$T)){ #This dont work :)
>
> print $$T{$id} . "\n";
> }
>
> }
>
> my %ht_state=("AL" => "Alabama","AK" => "Alaska");
> &printHT(\%ht_state);



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com