in reply to Hash problem

Dear Monks--

Solved it! Wasn't a hash/array problem at all -- though I'm still not sure why I couldn't print out a list of keys. Anyway, it's working now...

Thank you for all your comments: I know there are better ways of doing things; I'm trying to set the basic thing up first, & then I'll clean it up. What I don't understand is how my code is different from Larry Wall's in PROGRAMMING PERL. Here is his code:
for $group ( "simpsons", "jetsons", "flintstones" ) { @members = get_family($group); $HoA{$group} = [ @members ]; }
Do I need to dereference something here to get at it ?

For those who are wondering, @array2 has this kind of data:
"0451524934","1984",7.9500,67,"Penguin Putnam" "0618056815","42nd Parallel",13.0000,25,"Houghton Mifflin" "0195167929","A Strange Likeness - Becoming Red and White in Eighteent +h Century",29.9500,0,"Oxford University Press" "0520204026","Abortionist : A Woman Against the Law",25.0000,30,"Unive +rsity of California Press"

Later in my program I push stuff from @array1 onto each array in %Tex. At least in theory. It's weird how many examples, particularly of MySQL, use book information as examples, because that's what I am -- a bookseller.

jdporter added code tags around the data

Replies are listed 'Best First'.
Re^2: Hash problem
by graff (Chancellor) on Feb 05, 2006 at 16:15 UTC
    Well, there seem to be some similarities between the code snippet from the text book and the original code that you posted, but your original code didn't show anything that would set the contents of the "@bex" array. And neither your original statement of the problem, nor this later "clarification", has made it clear enough (to me, at least) what you are trying to do, or how that differs from what is actually happening.

    You say that @array2 contains "this kind of data":

    "0451524934","1984",7.9500,67,"Penguin Putnam" "0618056815","42nd Parallel",13.0000,25,"Houghton Mifflin" "0195167929","A Strange Likeness - Becoming Red and White in Eighteent +h Century",29.9500,0,"Oxford University Press" "0520204026","Abortionist : A Woman Against the Law",25.0000,30,"Unive +rsity of California Press"
    (Note that it is possible and preferable to use <code> tags around data samples as well as code snippets.)

    Looks like you want to parse each element as CSV data, because it's possible that some of the quoted fields may contain commas, and a simple regex will have trouble with that. Text::CSV will probably be what you want:

    use Text::CSV; # ... load data into @array2 my $csv = Text::CSV->new(); for my $line ( @array2 ) { if ( $csv->parse( $line )) { my @fields = $csv->fields; $Tex{$fields[0]} = \@fields; # or maybe you want this instead? : # $Tex{$fields[0]} = [ @fields[1..$#fields] ]; } else { warn "CSV parse error on: $line\n"; } }
    (I'm expecting that Text::CSV will remove the quotes that surround some field values, but if it doesn't, you can handle that easily enough.)