Solved

hash

Posted on 2006-10-31
4
424 Views
Last Modified: 2012-05-05
Test1.pm

sub new
{
 my $proto = shift;
 my $class = ref($proto) || $proto;

 my $this = {
        HASHARG    => &_generateList()   ### this function returens hash (return \%hash   (format is $hash{124} = david))
 };
  bless $this, $class;
 return $this;
}

sub _generateList {
 #blahhhh
 my %hash;
 $hash{123} = 'john';
$hash[456} = 'adam';
 return \%hash;
}


sub display{
 $this = shift;
 ### how can i display the keys and valus of HASHARG  in thsi prt of code
}
0
Comment
Question by:perlperl
  • 3
4 Comments
 

Author Comment

by:perlperl
ID: 17843238
i tried this it did not work

sub display
{
   my $this         = shift;
   foreach $k (keys %{$this->{HASHARG}} )
   {
      print "$k :::::: $this->{HASHARG}{$k} \n";
   }

}
0
 

Author Comment

by:perlperl
ID: 17843799
if i try to print in the new()
   foreach $k (keys %{$this->{HASHARG}} )
   {
      print "$k :::::: $this->{HASHARG}{$k} \n";
   }

it works fine...



but when i call $obj->display()
it does not print the vakue
0
 
LVL 10

Accepted Solution

by:
rivusglobal earned 500 total points
ID: 17843888
Hi perlperl,

If you are trying to debug a complex hash structure you could use Data::Dumper like this:

use Data::Dumper;

sub display {
    $this = shift;
    print Dumper( $this );
}

or you could just iterate through the key/value pairs like this:

sub display {
    $this = shift;
    foreach $k ( keys %$this ) {
        print "$k ::::: " . $this->{$k} . "\n";
    }
}
0
 

Author Comment

by:perlperl
ID: 17844100
actually the problem is solved..i was not calling the method properly

$obj->{TEST}->display()


i am havinf multiple inheritance...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now