in reply to Hash Array Help

You can't just push a hash onto an array and then expect to extract it back out, you'll need to push a reference to the hash onto the array (and you'll definitely want to make sure %rec is a lexical hash inside your read loop by declaring with my()):

my %rec = (state => $part1[0], char => $part1[1], stack_char => $part1[2], new_state => $part2[0], new_stack_char => $part2[1], ); push(@db, \%rec);

Now, when accessing these, you'll need to dereference them:

foreach my $hash (@db){ foreach my $key( keys %$hash){ print "KEY: $key \n"; print "VALUE: $hash->{$key} \n"; print "________________________\n"; } #end foreach $key }#end foreach $hash

For further info on references and nested structures, check these perldocs: perlreftut, perlref, perllol, and perldsc.

update: Hmm, perlreftut doesn't seem to be included in the perlmonks perldocs, but if you have 5.00503 (I think that's when it got included) or later you can read it on your machine.

Replies are listed 'Best First'.
Re: Re: Hash Array Help
by Anonymous Monk on Mar 11, 2001 at 11:51 UTC
    Hey guys,
    Thanks for the fast reply. I am still getting a weird output:
    HASH(0x140031c58) HASH(0x140031e28) HASH(0x140031e98) HASH(0x140031f08 +) HASH(0x\ 140031f78) HASH(0x140031fe8) KEY: state VALUE: 0 ________________________ KEY: new_state VALUE: 0 ________________________ KEY: stack_char VALUE: ________________________ KEY: new_stack_char VALUE: a ________________________ KEY: char VALUE: a

    Any ideas?

    thanks again in advance.
      Now that you are using hash references in your array (I assume), you must de-reference them when you use them. If your foreach loop variable is $hash, you must access the hash itself via %$hash

      buckaduck

      Which weird output? Are you talking about the HASH references? I don't see why the code you pasted would print that. Is there another print you added that you haven't told us about?