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, 3:59 am |
| Hei all,
how 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);
| |
| Gunnar Hjalmarsson 2007-09-22, 3:59 am |
| Santana wrote:
> In this example this foreach loop in "printHT" function dont work ,
> how is missed ?
Really? What output did you get, and what did you expect?
> #!/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);
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
| |
| Rodrick Brown 2007-09-22, 3:59 am |
| On 9/21/07, Santana <paulito.santana@gmail.com> wrote:
> Hei all,
> how 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);
>
Probably not the best solution but this works.
!/usr/bin/perl -w
use Data::Dumper;
my %ht_state=("AL" => "Alabama","AK" => "Alaska");
sub printHT {
foreach my $f (@_) {
while((my $k,$v) = each(%$f)) {
print "$k $v\n";
}
}
}
&printHT(\%ht_state);
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>
--
Rodrick R. Brown
http://www.rodrickbrown.com
| |
| John W. Krahn 2007-09-22, 7:59 am |
| Santana wrote:
> Hei all,
Hello,
> how 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($)
You are using a prototype. You shouldn't use prototypes.
http://library.n0i.net/programming/.../fm_prototypes/
> {
> 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);
The ampersand in front of the subroutine name means that the prototype will be
ignored. You shouldn't use an ampersand in front of a subroutine name.
perldoc perlsub
Your code works for me:
$ perl -e'
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);
'
Alabama
Alaska
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
| |
| Benkasminbullock@Gmail.Com 2007-09-23, 8:00 am |
| On Sep 21, 5:03 pm, paulito.sant...@gmail.com (Santana) wrote:
> Hei all,
> how 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);
Hmm? That actually does seem to work: I get
ben ~ 507 $ ./hashtest.pl
Alabama
Alaska
ben ~ 508 $
(I've called this script "hashtest.pl".)
Were you expecting some other thing to be output?
The version of perl is as follows:
ben ~ 508 $ perl --version
This is perl, v5.8.8 built for i486-linux-gnu-thread-multi
| |
| Nobull67@Gmail.Com 2007-09-23, 6:59 pm |
| On 22 Sep, 04:43, nore...@gunnar.cc (Gunnar Hjalmarsson) wrote:
> Santana wrote:
>
> Really? What output did you get, and what did you expect?
Yeah, I spent two minutes looking at the OP's code and could see no
reason why it would not do what it appeared to be trying to do.
Sure enough when I gave up and ran it, it did what it _appeared_ to be
intended to do.
| |
| JBallinger 2007-09-26, 10:00 pm |
| Is there anything to do with the fact that he is trying to pass a
"reference" hash?
On Sep 23, 7:55 am, nobul...@gmail.com (Nobul...@Gmail.Com) wrote:
> On 22 Sep, 04:43, nore...@gunnar.cc (Gunnar Hjalmarsson) wrote:
>
>
>
> Yeah, I spent two minutes looking at the OP's code and could see no
> reason why it would not do what it appeared to be trying to do.
>
> Sure enough when I gave up and ran it, it did what it _appeared_ to be
> intended to do.
|
|
|
|
|