########################################
sub ThingToHDF($$$;$);
sub ThingToHDF($$$;$) {
########################################
# Recursively add a structure of hashes, arrays and values to a HDF object.
my ($ref,$prefix,$hdfobj,$hdffile) = @_;
if (ref $hdffile eq '' and defined $hdffile and $hdffile) {
print "ThingToHDF - writing to $hdffile...";
$hdffile = OpenFile($hdffile,'w');
print "file handle $hdffile\n";
}
my $watchme = 0;
my $structured_hdf = 1;
my $hdfout = (ref $hdffile eq 'FileHandle');
(my $last = $prefix) =~ s/^.*\.// if $hdfout and $structured_hdf;
my $spacing = ' ' x ($prefix =~ tr/././) if $hdfout and $structured_hdf;
print STDERR "ThingToHDF($ref,$prefix,$hdfobj,",(defined $hdffile ? $hdffile : '[undef]'),")\n" if $watchme;
if (ref $ref eq '') {
$hdfobj->setValue($prefix,$ref);
if ($hdfout and $structured_hdf) {
print $hdffile "$spacing$last = $ref\n";
} elsif ($hdfout and not $structured_hdf) {
print $hdffile "$prefix = $ref\n";
}
} elsif (ref $ref eq 'ARRAY') {
$hdfobj->setValue($prefix,$prefix); # set presence of this level
if ($hdfout and $structured_hdf) {
print $hdffile "$spacing$last {\n";
}
foreach (0..$#{$ref}) {
ThingToHDF($ref->[$_],"$prefix.$_",$hdfobj,$hdffile) if defined $ref->[$_];
}
if ($hdfout and $structured_hdf) {
print $hdffile "$spacing}\n";
}
} elsif (ref $ref eq 'HASH') {
$hdfobj->setValue($prefix,$prefix); # set presence of this level
if ($hdfout and $structured_hdf) {
print $hdffile "$spacing$last {\n";
}
foreach (keys %$ref) {
ThingToHDF($ref->{$_},"$prefix.$_",$hdfobj,$hdffile);
}
if ($hdfout and $structured_hdf) {
print $hdffile "$spacing}\n";
}
} else {
warn "ThingToHDF: can't delve into a ",(ref $ref)," reference.\n";
}
}