I think noone commented so far on your second problem. So let me do that and look at your code first:

%ha = $obj -> passhash; foreach $key (keys (%ha)){ if ($key eq "problem2"){ foreach (@ha{$key}){ print "array : @{$_}\n"; } } }
The loop over all keys doesn't make much sense for a hash, after all, you use a hash to get easy access to its elements via the key (here: 'problem2'). So instead you would write: $ha{'problem2'}.

The entry of the hash %ha corresponding to the key 'problem2' is a reference to an array of arrays. To get to the values of that you have to dereference it: @{$ha{'problem2'}} or equivalent @$ha{'problem2'}.

Now you have an array which elements are references to arrays. You could print the elements of these (referenced) subarrays like this:

foreach my $arr_ref (@$ha{'problem2'}) { print "subarray: @{$arr_ref} \n"; }

Note: Your code works, if you replace the hash slice notation (@ha{$key}) with the dereferencing of the hash entry @$ha{$key}. The hash slice notation is normally used to get multiple values out of hash in one go, you (probably accidently) used it to extract just one value. This is then equivalent to $ha{$key}. An example of a good usage of a hash slice would be

my %hash; @hash{qw/one two three/} = qw/1 2 3/;

-- Hofmator


In reply to Re: two problems about passing var between classes by Hofmator
in thread two problems about passing var between classes by benlaw

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.