in reply to Use of Hash For Table Lookup

Data::Dumper does everything that you're trying to achieve:

use Data::Dumper; my %users = ( me => 1, you => 2, others => 3 ); #dump hash to file open my $file, ">", "temp~" or die $!; print $file Dumper \%users; close $file; #read hash data from file open $file, "<", "temp~" or die $!; my $hash_data = do { local $/; <$file> }; close $file; #eval it into %hash my %hash = do { no strict 'vars'; %{ eval $hash_data } }; print Dumper \%hash;

Output:

$VAR1 = { 'you' => 2, 'others' => 3, 'me' => 1 };

Have a good look at Data::Dumper. Other people have other favorites, but I think it's the third most useful module there is. strict and warnings being the top two, of course.

update: s/my $VAR1/no strict 'vars'/

Replies are listed 'Best First'.
Re^2: Use of Hash For Table Lookup
by country1 (Acolyte) on Aug 17, 2007 at 14:31 UTC

    My hash will have up to 1000 entries, so I need to read
    the CSV to create the %User hash. Can you give me any
    ideas or code samples that would show my how to do this
    with the code I included in my question?
      My hash will have up to 1000 entries,

      This is hardly relevant

      so I need to read the CSV to create the %User hash.

      This hardly follows from the previous remark.

      I still can't understand whether you have two separate programs because you have to or just because of a circumstance. Anyway, reading from the CSV and from the file created by your poorly reinvented D::D code wouldn't make such a difference: thus do you really need that intermediate passage?

      Can you give me any ideas or code samples that would show my how to do this with the code I included in my question?

      Well, it can't be done with the code you included in your question. Otherwise you wouldn't be asking here. OTOH several suitable suggestions have already been given to you, perhaps you should comment them explaining why you don't find them satisfactory.

      If you insist on using the CSV file instead of the good solutions already provided, I'd suggest looking into Text::xSV to read back the data in the CSV file and generate your hash in the 2nd script. Heck if you're going to write the file, I'd use it there as well.