in reply to How to initialize an array of hash references to hashes using literals

Can you give us something manageable to work with? No more than 10 lines should be needed to demo this problem.

\[ ... ] seems hinky, though. [ ... ] already returns a reference to the array created.

Replies are listed 'Best First'.
Re^2: How to initialize an array of hash references to hashes using literals
by jpietras (Initiate) on Feb 01, 2009 at 19:53 UTC

    I left the function but otherwise gutted all the code. I can gut the function a lot more but am concerned the I might not be able to implement the answer so I left it in

    #!/usr/bin/perl -w # use strict; ######################################## my %h00 = ( "--link" => "/etc/httpd/SSL", "--fold" => "/ +usr/local/private/QA/apache/SSL", ); my %h01 = ( "--link" => "/etc/httpd/conf/httpd.conf", "--file" => "/ +usr/local/private/QA/apache/conf/httpd.conf", ); my %h02 = ( "--link" => "/etc/httpd/conf.d/httpd.conf", "--file" => "/ +usr/local/private/QA/apache/conf.d/nagios.conf", ); my @a00 = ( \%h00, \%h01, \%h02, ); my %H00 = ( "--system", 'Apache', "--slinks", \@a00,); my @LINKS = ( \%H00,); my @LITERALS = ( '--system' => 'Apache', '--slinks' => \( { "--link" => "/etc/httpd/SSL", "-- +fold" => "/usr/local/private/QA/apache/SSL", }, { "--link" => "/etc/httpd/conf/httpd.conf", "-- +file" => "/usr/local/private/QA/apache/conf/httpd.conf", }, { "--link" => "/etc/httpd/conf.d/httpd.conf", "-- +file" => "/usr/local/private/QA/apache/conf.d/nagios.conf", }, ), ); print "LINKS ==============================\n"; &LinksLS(\@LINKS); print "LITERALS ==============================\n"; &LinksLS(\@LITERALS); ######################################## sub LinksLS() { my $aref = shift; my $aref_ndx; # index into "master" links array my $hash_ref; my $inner_hash_ref; my $sskey; my $system; my $slinks; my $nlinks; my $s; my $file; my $fold; my $link; for $aref_ndx (0..$#{$aref} ) { print '$$aref[ ' . $aref_ndx . '] = ' . $$aref[$aref_ndx] . "\n"; $hash_ref = $$aref[$aref_ndx]; $system = $hash_ref->{"--system"}; $slinks = $hash_ref->{"--slinks"}; $nlinks = @{$slinks}; $s = ($nlinks == 1) ? "" : "s"; print 'System ' . $system . " requires " . $nlinks . " link$s\ +n"; foreach $sskey ( keys %$hash_ref ) { print "\t\t" . $sskey . " is " . + $hash_ref->{$sskey} . "\n"; } undef $file; undef $fold; undef $link; foreach $inner_hash_ref ( @{$slinks} ) { print "\t" . $inner_hash_ref . "\n"; $link = $inner_hash_ref->{"--link"}; $file = $inner_hash_ref->{"--file"}; $fold = $inner_hash_ref->{"--fold"}; if ( not defined $link ) { die "$system --link is undfined, aborting!\n"; } elsif ( not defined $file and not defined $fold ) { die "$system both --file and --fold are undfined, abor +ting!\n"; } elsif ( not defined $file ) { print "\tFOLDER LINK: $link/ -> $fold/\n"; } elsif ( not defined $fold ) { print "\tFILE LINK: $link -> $file\n"; } } } }