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

I'm using Text::CSV_XS and it requires an array reference when printing output. I have a hash that I'd like to create an array reference for in order to create the output. I found this post (Perl packages to convert hash refs to array refs and vice-versa?) that shows a simple way to do it, but when I execute the following code I receive an error on the final line. Code:
my %errors = (); my $array_ref=[%$errors]; my $csvout = new IO::File "> csvoutput.csv"; my $status = $csv->print ($csvout, [%$errors])
Error:
Global symbol "$errors" requires explicit package name
Thanks for any help!

Replies are listed 'Best First'.
Re: hashref to arrayref
by jethro (Monsignor) on Jul 30, 2009 at 20:58 UTC
    Just use %errors instead of %$errors in this script. The syntax %$errors means "the hash $errors points to". Since you don't have a variable $errors initialized with anything resembling a pointer it is no surprise perl doesn't know what you want

    Note that $errors and %errors are totally different variables and have nothing but the name in common

Re: hashref to arrayref
by ssandv (Hermit) on Jul 30, 2009 at 21:07 UTC
    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);
      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!
      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?