in reply to hashref to arrayref

You're starting with a hash, not a hash ref, so you never declared $errors.
my $errors = {}; #or my $errors=\%existing_hash; my $array_ref=[%$errors]; my $csvout = new IO::File "> csvoutput.csv"; my $status = $csv->print ($csvout, [%$errors])
or
my %errors = (); my $array_ref=[%errors]; my $csvout = new IO::File "> csvoutput.csv"; my $status = $csv->print ($csvout, [%$errors])
should work.

Edit: this was a block copy to show what changed. I did not test it, and the last line in each case should be
my $status = $csv->print ($csvout, $array_ref);

Replies are listed 'Best First'.
Re^2: hashref to arrayref
by mcarthey (Novice) on Jul 31, 2009 at 13:34 UTC
    Thanks much for clarifying the use of the brackets and the references. I found part of my problem was my understanding of the square brackets. I thought they were also returning a reference (which they are) but only as evaluated at the time of the creation. I was using %errors before the hash was populated. I expected that the array reference would point to the populated list, but it did not. I guess it makes sense considering the brackets evaluate in list context and *then* return the reference. It seems to be working now, and I appreciate all your time! Thanks!
Re^2: hashref to arrayref
by mcarthey (Novice) on Jul 31, 2009 at 15:54 UTC
    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!
      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!