ksa1 has asked for the wisdom of the Perl Monks concerning the following question:

My example code gives me the following: The content of file2 is: ARRAY(0x1fd55b0) The content of file1 is: ARRAY(0x1fb5a68) The content of file3 is: ARRAY(0x1fd56d0)

Inside the sub I want to dereference the content of that ARRAY but even after reading a lot of other examples and trying out many solutions I did not find the right way to do it on my own... (instead of the 'ARRAY' it should show all values in there like host1, bla etc.) Also I want to be able to dynamically extend my orginal %hash with a new file-entry ($key) named file4 with its own values ($insert_val) in this example, I also had problems with that part (which is commented out atm). I hope anyone can help me... Many thanks in advance!
sub testfunc (\%) { my ( $href ) = @_; my $key = "file4"; my $insert_val="host1"; my $insert_val2="foo"; #push(@{$href{$key}}, $insert_val); #push(@{$href{$key}}, $insert_val2); foreach my $file (keys %$href) { print "The content of $file is:\n"; foreach my $line ( $href->{$file} ) { print "\t$line\n"; } } } my %hash = ( file1 => [ 'host1', 'bla', 'foo', ], file2 => [ 'host2', +'host1', '..', ], file3 => [ 'x', 'host2', 'host1' ] ); testfunc(%hash);

Replies are listed 'Best First'.
Re: How to dereference with sub hashes array...?
by hdb (Monsignor) on Apr 16, 2013 at 17:52 UTC

    Your output tells you that you are getting references to arrays. You only need to dereference them like this:

    foreach my $line ( @{$href->{$file}} ) {
Re: How to dereference with sub hashes array...?
by LanX (Saint) on Apr 16, 2013 at 17:53 UTC
Re: How to dereference with sub hashes array...?
by NetWallah (Canon) on Apr 16, 2013 at 19:50 UTC
    Your commented-out "push" statements would work with a slight modification:
    push @{ $href->{$key} }, "whatever"; #== Notice the de-reference operator here.

                 "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
            -- Dr. Cox, Scrubs

      Thanks all, everything works now! :)