in reply to Re: hashref to arrayref
in thread hashref to arrayref

Regarding creating the array reference, is it possible to completely output the contents of the hash rather than the reference value? The hash is being accessed like this:
$errors{$key}{'date'} $errors{$key}{'time'}
If I do the following I get a hash reference followed by the key value.
my $array_ref = [%errors]
If I do this,
my $array_ref = [keys %errors]
then I get only the keys, as you'd expect. Is there a way to generate the array to contain each element or will I have to resort to a looping construct? Thanks again!

Replies are listed 'Best First'.
Re^3: hashref to arrayref
by ssandv (Hermit) on Jul 31, 2009 at 16:09 UTC
    I'm not clear on what you want the array to contain. Just the bottom-level values? If I understand correctly, your structure looks like
    %errors=(key1 => { date => 'some date', time => 'some time', }, key2 => { date => 'some other date', time => 'some other time', }, ... );
    so when you read it in list context, you get a list of keys and hashrefs. What exactly are you trying to get back?
      I ended up doing the following to get what I needed:
      my @values = map { ( $errors{$_}{'date'}, $errors{$_}{'time'}, $errors{$_}{'cnt' +}, $_ ) } keys %errors; my $value_ref = [@values]; my $file_ref = new IO::File "> csvoutput.csv"; my $status = $csv->print ($file_ref, $value_ref)
      The problem was that in my original code I was returning array references to the elements in the @values array. I simply had to remove the brackets and all is good. Thanks again for all your time!