http://qs321.pair.com?node_id=75531


in reply to Getting information from an array of hashes

You should probably read perldoc perldsc so that you understand why your code is wrong.

In a nutshell, it's because anything inside a data structure is a scalar -- either a plain scalar or a reference (e.g. to an array or a hash). There are no arrays inside arrays, only arrayrefs inside arrays (or inside hashrefs inside arrays, or... etc.).

hdp.

Replies are listed 'Best First'.
Re: Getting information from an array of hashes
by chumley (Sexton) on Apr 26, 2001 at 01:16 UTC

    Thanks for the reference. I've looked through it, and there's some good stuff there to digest. However, one of the examples in perlman:perldsc is almost identical to something I already tried, following an example in Programming Perl.

    for ($i = 0 ; $i <= $#diary_data ; $i++ ) { print "$i is { "; for $temp_keys ( keys %{ $diary_data[$i] } ) { print "$temp_keys=$diary_data[$i]{$temp_keys} "; } print "}\n"; }

    This still generates an error. I broke it down to do one step at a time, like this:

    for ($i = 0 ; $i <= $#diary_data ; $i++ ) { print "I: $i\n"; for $temp_keys ( keys %{ $diary_data[$i] } ) { print "Key: $temp_keys\n"; print "Data: $diary_data[$i]{$temp_keys}\n"; } }

    This gives

    I: 0 Key: timestamp Use of uninitialized value at ./send_referral.pl line 197. Data: Key: value Argument "NWORLASTE800 LOGIN FAILED\nNWORLASTE801 LOGIN FAIL\n\nWA..." + isn't numeric in helem at ./send_referral.pl line 197. Bad index while coercing array into hash at ./send_referral.pl line 19 +7. $

    However, if I comment out the line that starts with print "Data...", it prints the correct keys for this hash:

    I: 0 Key: timestamp Key: value Key: user

    What confuses me is the fact that I'm following examples straight out of Perl references and getting these errors. I'm starting to wonder if I've found a bug in ARS.pm.

    Chumley
      "However, one of the examples in perldsc is almost identical to something I already tried, following an example in Programming Perl."

      I'm not sure that's true. What it seems you're trying to do -- i.e. take an arrayref and turn it into a hash -- isn't really covered in perldsc. The reason I pointed you to it was because of my %hash = $diary_data[0] and the accompanying error about Reference found where even-sized list expected, not because of anything in the other suggestions people made or other things you'd tried; I'm sorry if there was confusion there.

      What happens if you try %hash = %{$diary_data[0]->[0]}? (This is a bit of a shot in the dark based on some of your other posts.)

      "What confuses me is the fact that I'm following examples straight out of Perl references and getting these errors. I'm starting to wonder if I've found a bug in ARS.pm."

      You may be right, but it's probably just a nested data structure that you haven't quite understood yet. Data::Dumper will help a lot, if that's the case (print Dumper($diary_data[0])).

      hdp.

        It's not that I'm trying to turn an arrayref into a hash, I'm trying to take a function that is supposed to return an array of hashes and get the data out of the hashes. Sorry if I wasn't as clear as I could be. (perldsc calls this a "list of hashes", which I thought would be the same thing. Maybe not?)

        It may well be the same thing as a nested data structure, and it's quite likely that I don't understand it yet. Happens to me a lot - that's why I have a shelf full of O'Reilly books!

        Chumley